File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Thread Safety in Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Thread Safety in Servlets" Watch "Thread Safety in Servlets" New topic
Author

Thread Safety in Servlets

somasundar venkatesh
Greenhorn

Joined: Mar 01, 2011
Posts: 14
Hi,

can anybody help me in this issue..as I'm new to Servlets.

How the Servlets are providing the thread-safety feature, even though it can be accessed by multiple threads at a time and even SingleThreadedModel concept has been removed ?


Thanks and Regards,
Somasundar.
hemantha kumar k
Greenhorn

Joined: Apr 20, 2010
Posts: 21

Servlet is not Thread safe, so it's all about programming, if we want to make servlet as thread safe the use the synchronize block to synchronize only part of the code which you intended inside the doGet or doPost methods.
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50652

tinku rider wrote:if we want to make servlet as thread safe the use the synchronize block to synchronize only part of the code which you intended inside the doGet or doPost methods.

No, that's a bad idea and something that's only done as a last resort.

Rather, most servlets can be made thread0safe by simply being sure not to use any instance variables.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
abani patra
Ranch Hand

Joined: Oct 11, 2011
Posts: 70
Hi,
If I need instance how it will be threadsafe without synchronize block.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2317
Java has other ways of providing synchronization; start reading here: http://download.oracle.com/javase/tutorial/essential/concurrency/index.html

But there's nothing wrong with using explicit synchronization if the circumstances call for it.
somasundar venkatesh
Greenhorn

Joined: Mar 01, 2011
Posts: 14
Tim Moores wrote:Java has other ways of providing synchronization; start reading here: http://download.oracle.com/javase/tutorial/essential/concurrency/index.html

But there's nothing wrong with using explicit synchronization if the circumstances call for it.


But if we use explicit synchronization then again that concept is same as SimpleThreaded Model right. Then why is that removed from Servlets?
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50652

Because it causes more trouble that it solves.

Avoiding the use of instance variables solves 99% of all threading issues. In over 10 years of writing servlets I've never had to use a synch bock.
somasundar venkatesh
Greenhorn

Joined: Mar 01, 2011
Posts: 14
Bear Bibeault wrote:Because it causes more trouble that it solves.

Avoiding the use of instance variables solves 99% of all threading issues. In over 10 years of writing servlets I've never had to use a synch bock.



Hi Bear, got it now. Thanks for your reply and thanks for all who have replied.



Regards,
somu
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2317
somasundar venkatesh wrote:But if we use explicit synchronization then again that concept is same as SimpleThreaded Model right.

No. Synchronization can be used for code blocks as small as you like, whereas STM effectively synchronizes the entire code of a servlet by ensuring that there is no concurrency.

STM was removed because it does not solve all concurrency issues as people seemed to assume, and -even worse- completely killed concurrency; that's not what you want in a web app. There is no shortcut to learning about the multiple concurrency options in Java if you're serious about web apps. A book like "Java Threads" or "Concurrency in Practice" will help tremendously with that.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 694
i read this thread, but i want to know what exactly is the problem of thread in servlet?
is it mean that if i use thread methods in servlet, they are not safe?
isn't it??
 
 
subject: Thread Safety in Servlets
 
Threads others viewed
Single Thread Model.
SimpleDateFromat class problem
Regarding Thread-safe in Servlets
to Ulf Dittmer about SingleThreadModel Interface
Struts 1.2 session problem
IntelliJ Java IDE