*
The moose likes Servlets and the fly likes Is a Servlet thread-safe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is a Servlet thread-safe" Watch "Is a Servlet thread-safe" New topic
Author

Is a Servlet thread-safe

p hasini
Ranch Hand

Joined: Oct 24, 2009
Posts: 92
Is a servlet thread safe.If yes,how.
If not,how to make a servlet thread safe.

When there are multiple requests for multiple servlets,how is this handled.

When there are multiple requests for a single servlet,how is this handled
Tom Kowalski
Ranch Hand

Joined: Feb 17, 2009
Posts: 72
Servlets are not thread safe. If you want to make it Servlet as Thread safe, you can implement SingleThreadInterface which is a blank Interface there is no methods (this is not recomend method, because it could slow the performance of your page) or you can synchronize methods by using synchronized keyword. Anyway, this is quite basic question, try to use google in this case, you will find a lot of pages with a full story about this .


SCJP, SCWCD, OCUP
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

The SingleThreadModel interface is deprecated and due for removal. The problem is that it causes a performance hit while only providing the illusion of thread safety.
Assume that multiple threads will be able to access your servlet at any time (except during initialisation). Therefore local variables are safe but instance variables are not. The Request and Response are (typically) thread safe but any other shared or global instance such as Contexts, sessions etc are not and must be treated with care.
p hasini
Ranch Hand

Joined: Oct 24, 2009
Posts: 92
I searched in googled.But I didnt get a clear idea.

SingleThreadModel interface is deprecated from which version.

How can we make a servlet thread-safe without using SingleThreadModel.

Which methods do we synchronize in the servlet class.

As far as I know,we need to declare local variables not instance variables in our class.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Deprecated from Servlets 2.4 (see the link to the interface and read the API)
There is no simple answer to making a Servlet thread safe. Making sure you don't have any instance or mutable static variables is a start, local variables are a safe replacement.
The full answer is that it is no longer a Servlet based problem but a threading problem.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41064
    
  43
If you're at all serious about concurrency in Java -and in these days of multi-core CPUs, multiple CPUs per machine, and multi-threaded environments like servlet containers and application servers, who can afford not to be?- I'd advise to work through one of the eminent book on the subject - either "Java Threads" by Oaks/Wong or "Java Concurrency in Practice" by Brian Goetz et al.


Ping & DNS - my free Android networking tools app
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I haven't read Java Threads but should, I read Java Concurrency in Practice cover to cover. Recommended read.
p hasini
Ranch Hand

Joined: Oct 24, 2009
Posts: 92
To make things simple,can we synchronize service() method.
or should we synchronize only the doGet() and doPost() methods
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41064
    
  43
A servlet should not override the service method, so you can't really synchronize that. Note that synchronizing the entire doGet or doPost methods effectively destroys concurrency for this servlet; that may be OK if there are multiple servlets and the one in question is only a small part of the web app, but generally you don't want to do that.

It might be OK to synchronize small, appropriate sections of those methods.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd certainly look very, very carefully at the data you believe you need to synchronize, and see if it really needs to be synchronized per-request.
James Gadbury
Greenhorn

Joined: Mar 20, 2009
Posts: 24
Sorry to bump an old thread but I wanted to post a link to an excellent article I just read on Javaworld which would be most useful to others that find their way here.

Write thread-safe servlets
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

Really that is a nice article to understand. Some times before I've had some thread sharing problem and I've solved using inner class to initialize variables that are needed. This stops thread sharing of instance variables and it's up to your requirement.


No pain, No gain.
OCJP 1.6
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
While that article demonstrates the various approaches to thread safety, I don't particularly like the example. You wouldn't use synchronization for protecting an int, you'd use an AtomicInteger instead.
 
 
subject: Is a Servlet thread-safe
 
Similar Threads
SIngleThreadModel
SingleThreadModel ??
SingleThreadModel
Thread safety
Urgent : Thread Safe