Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle concurrency in java servlets

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 using this at the same time ?

Regards, Mithun

like image 514
Vidya Avatar asked Sep 25 '26 13:09

Vidya


2 Answers

  • 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.
like image 58
Marko Avatar answered Sep 28 '26 01:09

Marko


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.

like image 42
Licky Lindsay Avatar answered Sep 28 '26 01:09

Licky Lindsay