aspose file tools*
The moose likes Servlets and the fly likes Is Servlet threadsafe ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is Servlet threadsafe ? " Watch "Is Servlet threadsafe ? " New topic
Author

Is Servlet threadsafe ?

nagraj patel
Greenhorn

Joined: Feb 16, 2013
Posts: 18

i know this answer not? but one of my interview this question is asked
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

Which servlet? It depends how the servlet is written.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
nalla srinivas
Greenhorn

Joined: Jan 10, 2013
Posts: 9
yes
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2368
    
    7

nalla srinivas wrote:yes


Not true. In order to write thread-safe servlets, you need explicitly make it thread-safe by either synchronizing the code block or implement the SingleThreadModel (which I think is deprecated). By default, servlets are multithread apps.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
M Mohile
Greenhorn

Joined: Sep 22, 2012
Posts: 17
Please refer http://www.coderanch.com/t/473015/Servlets/java/Servlet-thread-safe.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

K. Tsang wrote:
nalla srinivas wrote:yes


Not true. In order to write thread-safe servlets, you need explicitly make it thread-safe by either synchronizing the code block or implement the SingleThreadModel (which I think is deprecated).

Also not accurate. It's a very bad idea to either use synchronization, or to use the deprecated SingleThreadModel.

Rather, the servlet needs to be written in a thread-safe manner. 99% of that is avoiding instance variables.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41509
    
  53
Bear Bibeault wrote:It's a very bad idea to [...] use synchronization

No. Like everything, synchronization can be misused, but there's nothing wrong with it to make an absolute statement like this true. It's one of several features in Java to make concurrency work. I would agree to something like "it's a bad idea to synchronize your doGet and doPost methods", because then you'd end up with serialized execution.

Java concurrency (and especially so in web apps) is a tricky -and important- subject, though. I recommend that anyone should work through a good book like Oaks/Wong or Goetz et al.


Ping & DNS - my free Android networking tools app
shivam singhal
Ranch Hand

Joined: Jul 15, 2012
Posts: 226

Servlets are composed with lots of things like..
1. Request scope, Context scope , Session scope
2. Instance variables , local variables
so, it depends on you..weather to make your servlet thread safe or not..
following things you have be in your mind while designing a Thread Safe Servlet

1. Mostly try to use Local variables.. as they are thread safe.. while instance variables are not
2. try to pass control and data in via request scope.. because its thread safe

* Yeah, Single Thread Model can basically synchronise your full servlet.. and it is not a GOOD idea , as due to this your servlet can process one request at a time, which is BAD .. it will leads to Delay in request processing when your site is rising up with large hits..

With Regards
SHIVAM SINGHAL
OCPJP - 82%
OCPJWCD - 97%
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

Ulf Dittmer wrote:Like everything, synchronization can be misused, but there's nothing wrong with it to make an absolute statement like this true.


Agreed, my reply missed a word; I meant to say that it's a bad idea to use synchronization indiscriminately.
Joe Ridener
Ranch Hand

Joined: Jul 16, 2012
Posts: 86
Per Chapter 3 of Core Servlets:

Normally, the system makes a single instance of your servlet and then creates a new
thread for each user request. This means that if a new request comes in while a previous
request is still executing, multiple threads can concurrently be accessing the
same servlet object. Consequently, your doGet and doPost methods must be careful
to synchronize access to fields and other shared data (if any) since multiple
threads may access the data simultaneously. Note that local variables are not shared
by multiple threads, and thus need no special protection.

I think that should answer your question.

Simply put... the same as any class multiple threads can access these applications. If you they share resources you may want to consider calling locks on these objects to perform operations on them by using synchronized methods or the

synchronize(objectYourConcernedAbout)
{
//operations to be performed on this object

}

within a method.. the same as any other application.
nagraj patel
Greenhorn

Joined: Feb 16, 2013
Posts: 18
Bear Bibeault wrote:
K. Tsang wrote:
nalla srinivas wrote:yes


Not true. In order to write thread-safe servlets, you need explicitly make it thread-safe by either synchronizing the code block or implement the SingleThreadModel (which I think is deprecated).

Also not accurate. It's a very bad idea to either use synchronization, or to use the deprecated SingleThreadModel.

Rather, the servlet needs to be written in a thread-safe manner. 99% of that is avoiding instance variables.

yes Bear,
Servlets are by default not threadsafe we can make threadsafe by using SingleThreadModel or using synchronizing code block.again my question for you how to make servlet garbage collected explicitly ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41509
    
  53
What do you mean by "make servlet garbage collected explicitly"? All objects are eligible for GC once there are no longer references to them. At which point they actually do get GCed is beyond your control, and shouldn't matter anyway. What are you worried about?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
we can make threadsafe by using SingleThreadModel


Not true - having a single instance per request Thread does not prevent an inept programmer from bad practices when accessing other objects or static variables.

The original designers thought SingleThreadModel would make it easier for programmers moving from single user desktop applications to multiuser shared applications - it didn't work out.

Bill
Mr. Ankur Garg
Greenhorn

Joined: May 27, 2012
Posts: 6

nope.. because it may have session attribute,
context attribute which can live for multiple
request in which case attributes may become
inconsistent.

Ankur Garg (SCJP,SCWCD)
Surapuraju Venkatakishor
Greenhorn

Joined: Dec 27, 2011
Posts: 11

Servlets are not Thread Safe. Servlet instances are inherently not thread safe because of the multi threaded nature of the Java programming language in general. The Java Virtual Machine supports executing the same code by multiple threads. This is a great performance benefit on machines which have multiple processors. This also allows the same code to be executed by multiple concurrent users without blocking each other.

Imagine a server with 5 processors wherein a normal servlet can handle 500 requests per second. If that servlet were threadsafe, then the web application would act like as if it runs on a server with 1 processor wherein the servlet can handle only 100 requests per second (okay, it's not exactly like that, but you got the idea).

If you encounter threadsafety issues when using servlets, then it is your fault, not Java's nor Servlet's fault. You'd need to fix the servlet code as such that request or session scoped data is never assigned as an instance variable of the servlet.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Servlet threadsafe ?