Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Making servlets thread-safe  RSS feed

 
Pramod Chris
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet that is servicing client requests from another application. I'm simply reading their requests and sending a response back depending on their requests (All in form of Strings).I'm making calls to a couple of methods in my local Application to generate the responses. Now, considering that there can be numerous such client requests, do I have to explicitly take care of making my servlet thread-safe or will the servlet container take care of it? Will it see to it that only one client request is handled at a time or do i have to explicitly synchronize my code in the doGet() method?
 
louise rochford
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
eeek! Don't synchronize the doGet() unless you really want all requests to the servlet to get queued up. The servlet container will (normally) create one instance of the servlet & run multiple requests through that one instance at the same time. To make things thread safe you need to ensure you only use local variables in the servlet code (declared inside the methods) or variables that are in the request itself.
(In case you're thinking about it, don't be tempted to implement SingleThreadModel either - it's kind of the amse as synchronizing the doGet())

Cheers,
Louise
 
Pramod Chris
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Louise, so since I don't have any instance variables or static variables which would have been created for every thread accessing the servlet,I don't need to handle anything myself, the servlet container would handle different requests for me right? Then my next question would be..when would one ideally use explicit synchronization inside the doGet() or doPost() methods? Thanks again..
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

when would one ideally use explicit synchronization inside the doGet() or doPost() methods?


when some instance or class level methods or variables are used inside service methods, doPost() or doGet().
[ December 10, 2004: Message edited by: Adeel Ansari ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be real picky:
I don't have any instance variables or static variables which would have been created for every thread accessing the servlet

You've done the right thing - not having any of those - but given the reasoning exactly backwards. Any instance or static variables are SHARED by all threads. That may have been just what you meant to say ... but not how I read it. Anyhow, avoiding member and static variables is an essential step in making your code thread safe so you're well on your way!
 
louise rochford
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Adeel says.
I suppose it would be acceptable to sync in doGet() / doPost() if you're sure that the performance of your servlet isn't an issue. It sounds like this might be the case for you since you mention calling it from another app (rather than the more usual situation of calling it from a browser across the internet). Say if the calling app sends in requests infrequently, and when it does, it doesn't matter too much how long the call takes, so long as threads aren't crossed. Even then it'd probably be easier (& more efficient processor-wise) just to code the servlet in a thread-safe manner as good practice. (You never know when someone less experienced may use your code in the future in a different situation. I guess you could say that about all code, but I prefer to try to do things as 'correct' as possible if / when time allows, just in case...)

Louise
 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best possible ways to make synchronize a instance variable in the servlet is do the changes in the synchronize method of the servlet for example if you are having i has the instance variable then inside the doGet/doPost method:


synchronize(this)
{
i++;
out.println(i);
}


if you are doing the changes in any object then

it should be

synchronize(object)
{
----- do the changes;
}

Please let me correct if it is wrong.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!