A servlet that can handle requests from multiple threads concurrently without its internal state (or that of the request, response, session, etc; attributes really need to be looked at carefully) getting in an inconsistent state.
A servlet can handle concurrent requests in a same time.That situation one can modify the instance data while other person has to work on that same inside service().Here deadlock could be occured.so,we need to avoid that situation with the help of thread safe (i.e synchronized keyword).
The synchronized keyword should be avoided unless absolutely necessary. There are better ways to make servlets thread safe. Not using instance variables for example. In over a dozen years of writing servlets, I've never had to use synchronized even once.
I go with Bibeault word, I've never used synchronized in methods or in blocks. Used instance variables and as already explained here got overwriting problem between requests on instance variables. Now changed all instance variables into inner class and initiating this class for all requests, by this can be avoided the situation of overwriting and synchronization.
No pain, No gain.
Joined: Sep 21, 2011
While one shouldn't synchronize entire methods, I see no reason to try and avoid the synchronized keyword entirely for code blocks. Sometimes that is the best way to ensure thread safety, for varying definitions of "best". In order to make an informed decision what the best approach is I recommend to work through one of the eminent books on Java concurrency, either Java Threads by Oaks/Wong or Concurrency in Practice by Goetz et al.