File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Multithreaded service() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multithreaded service()" Watch "Multithreaded service()" New topic

Multithreaded service()

Steven Lau

Joined: Mar 25, 2002
Posts: 9
if service() method of a typical servlet is multithreaded, which one of the following issues does not need to be address in the servlet's implementation?
Concurrent access to =>>shared resouces ?
local variables?static variables? instance variables?

[ April 01, 2002: Message edited by: Roland2k1 ]
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
1. Access to shared resouces outside the servlet is a concern, even if you are implementing SingleThreadModel, because all that is guaranteed is that only one thread will execute the service() method at a time.
2. Local variables are as thread-safe as they can be, since each thread as its own "copy".
3. Static variables are not thread-safe, even if you implement SingleThreadModel, because the specs allow the container to create a pool of servlets.
4. Instance variables are not thread safe. Even though only one servlet instance (per servlet definition) services requests, concurrent requests will spawn threads that can access that servlet's instance variables.
You can check out more of the specs here. It's actually very readable and informative
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Your display name does not conform with the Javaranch naming policy. Please read this policy and change your display name if you wish to keep posting here. Thanks.

Matthew Phillips
I agree. Here's the link:
subject: Multithreaded service()
jQuery in Action, 3rd edition