Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Thread Safety in Servlets

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
Hibernate jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If I need instance how it will be threadsafe without synchronize block.
 
Saloon Keeper
Posts: 7379
170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7379
170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic