• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread safe servlet

 
shivdhwaj pandey
Ranch Hand
Posts: 67
Java PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is thread safe servlet?
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
shivdhwaj pandey
Ranch Hand
Posts: 67
Java PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor thanks but can you please explain it more
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can search for it. It basically means your code should be able to handle multiple concurrent requests without any issues. One thing that will help is to not use instance variables.
 
Pene charl
Greenhorn
Posts: 21
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Moores
Bartender
Posts: 2752
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic