开发者

how to handle concurrency in java servlets

开发者 https://www.devze.com 2022-12-17 06:22 出处:网络
In my doPost method of the servlet I need to access a file (shared resource ) and update the file . How do I cater to some 100 users usin开发者_如何学编程g this at the same time ?

In my doPost method of the servlet I need to access a file (shared resource ) and update the file . How do I cater to some 100 users usin开发者_如何学编程g this at the same time ?

Regards, Mithun


  • Create separate singleton class for file access.
  • Use Read and WriteLock from java.util.concurent package to protect file access.
  • Cache, so that you won't have to do file read if all you have to do is to return file content.


Are you sure a file is how you want to handle this? Protecting access to data by multiple concurrent users is most of what a modern database does.


With high level of concurrency (for writes), synchronizing will cost you a lot of throughput.

Databases are more adequate to handle this, if possible in your project.


I would take advantage of the java.util.concurrent packages added in Java 1.5. Specifically a BlockingQueue to queue up requests and handle them in a second pool of threads.

0

精彩评论

暂无评论...
验证码 换一张
取 消