Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Safety in Servlets  RSS feed

 
somasundar venkatesh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
Hibernate Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
abani patra
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If I need instance how it will be threadsafe without synchronize block.
 
Tim Moores
Saloon Keeper
Posts: 3512
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 3512
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1028
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!