37

I need to know why the servlets are not thread safe ? And whats the reason that in Struts 2.0 framework controller servlet is thread safe ?

Roman C
  • 49,761
  • 33
  • 66
  • 176
ammu
  • 864
  • 2
  • 13
  • 27
  • who told you that servlets are not thread safe? – bmargulies Mar 04 '12 at 14:55
  • @bmargulies *I* sure wouldn't call them "thread-safe", although I'd caveat that it depends entire on implementation. Conventional wisdom calls them not thread safe. – Dave Newton Mar 04 '12 at 15:05
  • Related: http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading – BalusC Mar 04 '12 at 16:51
  • Welcome to Stack Overflow. Please read [How to Ask](http://stackoverflow.com/questions/how-to-ask), [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/), and [How To Ask Questions The Smart Way](http://catb.org/esr/faqs/smart-questions.html). –  Mar 05 '12 at 13:08

6 Answers6

51

I need to know why the 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 4 processors wherein a normal servlet can handle 1000 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 250 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. For an in-depth explanation, see also How do servlets work? Instantiation, sessions, shared variables and multithreading.

And whats the reason that in Struts 2.0 framework controller servlet is thread safe ?

It is not thread safe. You're confusing the Struts dispatcher servlet filter with Struts actions. The struts actions are re-created on every single request. So every single request has its own instance of the request scoped Struts action. The Struts dispatcher servlet filter does not store them as its own instance variable. Instead, it stores it as an attribute of the HttpServletRequest.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
22

Servlets are normal java classes and thus are NOT Thread Safe.

But that said, Java classes are Thread safe if you do not have instance variables. Only instance variables need to synchronize. (Instance variable are variables declared in the class and not in within its methods.

Variables declared in the methods are thread safe as each thread creates it own Program Stack and function variables are allocated in the stack. This means that variable in a methods are created for each thread, hence does not have any thread sync issues associated.

Method variables are thread-safe, class variables are not.

Charith De Silva
  • 3,650
  • 4
  • 43
  • 47
Linus
  • 880
  • 13
  • 25
  • 4
    Not quite sure with _Only instance variables need to synchronize_ . Static variables also need synchronization and they are not instance variables. – Paramvir Singh Karwal Sep 20 '18 at 06:48
  • This is false and needs to be corrected as per @ParamvirSinghKarwal ' s comment. In java variables that are declared at the class level (not method level) are classified into two categories: static variables (aka class variables) and instance variables (those that belong to the object). It is confusing to say: "Only instance variables need to synchronize." when you mean "Only variables declared at the class level (static and instance variables) need to synchronize." - your intention only becomes clear after reading the full answer. – Mindaugas Bernatavičius Dec 26 '20 at 08:47
11

There is a single instance of a servlet per servlet mapping; all instance properties are shared between all requests. Access to those properties must take that in to account.

Struts 2 actions (not "controller servlet", they're neither servlets nor controllers) are instantiated per-request. Action properties will be accessed only by a single request's thread.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
2

Servlets are normally multi-threaded.

Servlet containers usually manage concurrent requests by creating a new Java thread for each request. The new thread is given an object reference to the requested servlet, which issues the response through the same thread. This is why it is important to design for concurrency when you write a servlet, because multiple requests may be handled by the same servlet instance.

The way that servlet containers handle servlet requests is implementation dependent; they may use a single servlet, they may use servlet pooling, it depends on the vendor's system architecture.

Struts 2 Action objects are instantiated for each request, so there are no thread-safety issues.

John Eipe
  • 10,922
  • 24
  • 72
  • 114
1

Servlet is not thread safe but we can make it as a thread safe by implementing that servlet class to SingleThreadModel like the given below class definition but again the performance problem will be there so better option would be use synchronized portion

public class SurveyServlet extends HttpServlet
                           implements SingleThreadModel
{
servlet code here..
...
}
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
user2663609
  • 407
  • 7
  • 10
0

Servlet is not thread-safe by itself. You can make it thread-safe by making the service method synchronized. you need to implement SingleThreadInterface to make it thread-safe.

lolo
  • 17,392
  • 9
  • 25
  • 49
  • If a servlet was not thread-safe, how would all the Java web aps out there work flawlessly, even without making them implement SingleThreadInterface? They're not thread-safe if you implement them incorrectly. They're thread-safe if you make them so. – JB Nizet Mar 04 '12 at 17:13