I am working on a servlet and i want to make my servlet synchronized.So please any can help me that how it can be possible.
-
1It would be better to explain your real problem. Note that by synchronizing a single servlet used in multiple threads **will** impact on your application performance. – Luiggi Mendoza Oct 22 '13 at 05:21
-
You almost certainly *don't* want your servlet synchronized. You want it to be able to serve as many concurrent clients as possible. Any synchronization should be reduced to the absolute minimum to get the job done. – user207421 Oct 22 '13 at 05:24
-
1I highly recommend you to read http://stackoverflow.com/q/3106452/1065197 and to rewrite your question explaining your real problem in order to get more accurate help. – Luiggi Mendoza Oct 22 '13 at 05:26
-
One small question realizes answer. If user1 accessing your application and user2 want's to do same job at same. If you synchronize, User2 needs to be wait until User1 finishes his task. Do you want it ?? Any user wants to use such type of Application ? – Suresh Atta Oct 22 '13 at 05:27
3 Answers
Making a servlet synchronized is a very bad design. The main purpose of it will be destroyed. Servlets should be designed in such a way that it can process multiple number of requests simultaneously! Moreover, the servlet should not contain any state storage and press the need for synchronization. Please rethink your design

- 12,760
- 2
- 32
- 53
Anytime you synchronize blocks of code, you introduce bottlenecks into your system. When you synchronize a code block, you tell the JVM that only one thread may be within this synchronized block of code at a given moment. If we run a multithreaded application and a thread runs into a synchronized code block being executed by another thread, the second thread must wait until the first thread exits that block.
It is important to accurately identify which code block truly needs to be synchronized and to synchronize as little as possible.
Note that you need not (and should not) synchronize on local data or parameters. And especially you shouldn't synchronize the service() method! (Or doPost(), doGet() etc.)
What's a better approach for enabling thread-safe servlets ? SingleThreadModel Interface or Synchronization?
Although the SingleThreadModel technique is easy to use, and works well for low volume sites, it does not scale well. If you anticipate your users to increase in the future, you may be better off implementing explicit synchronization for your shared data. The key however, is to effectively minimize the amount of code that is synchronzied so that you take maximum advantage of multithreading.
Also, note that SingleThreadModel is pretty resource intensive from the server's perspective. The most serious issue however is when the number of concurrent requests exhaust the servlet instance pool. In that case, all the unserviced requests are queued until something becomes free - which results in poor performance. Since the usage is non-deterministic, it may not help much even if you did add more memory and increased the size of the instance pool.

- 8,402
- 13
- 54
- 89
Making a servlet synchronized is really a bad design. You cannot handle more that one client at a time. I suggest you to make a block of code synchronized if you want some part of your code to be thread-safe. Please do have a look to here

- 2,278
- 11
- 23