• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread safety in Servlets

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Here's my understanding of handling thread safety wrt Servlets. The question follows..

For each client request, the Servlet container spawns a new thread which inturn calls the service() method of the Servlet. Lets say, the thread has a run() method within which there's a statement like: servlet.service(request, response); This is all taken care of by th servlet container. As a programmer, our job is to make sure the doGet()/doPost() are thread-safe.

Now, consider a general situation where we have a thread class, multiple instances of which are created in some other class. Lets say, under the run() method of this thread, there is a call to some library function in the Java API (similar to how the service() of Servlet gets called). In this case, how do we ensure that the Java API function is thread-safe? In general, is the Java API designed to handle thread safety?

I hope I made myself clear. Have I got something fundamentally wrong, or is this a valid question?

Thanks!
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manjusha, welcome to javaranch.

Not all the java API classes are thread safe. Like StringBuilder is not thread safe but StringBuffer is, ArrayList is not thread safe but Vector is. So you'll have to ensure thread safety in such cases. But this only applies to a single instance of the API class. If I have one StringBuilder object which is access simultaneously by multiple servlets or multiple threads in the same servlet, then thread-safety is needed on the programmers end.

HTH
 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To avoid thread problems in servlets, you can simply don't have instance variables, right?

YourServlet {
int x; // unpredictable results
}
 
Manjusha Maddala
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Ankit and Leandro.
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.javaworld.com/javaworld/jw-07-2004/jw-0712-threadsafe.html

hth
reply
    Bookmark Topic Watch Topic
  • New Topic