Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

The SingleThreadModel Interface

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone shed some lights on The SingleThreadModel Interface.
Please....
 
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dinesh,

SingleThreadModel can be made implement by servlets/configured for JSP to process the request in synchronized way. Instance variable which are being declared in servlets/JSP are not synchronized , so inorder to make them so we can implement the interface or configure JSP as threadSafe="false".
 
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

SingleThreadModel can be made implement by servlets/configured for JSP to process the request in synchronized way. Instance variable which are being declared in servlets/JSP are not synchronized , so inorder to make them so we can implement the interface or configure JSP as threadSafe="false".



While this goes into the right direction, I think it pays to be a bit more specific. Particularly the word "synchronized" has several meanings which should be disambiguated.

Implementing the STM interface does not make instance variables synchronized in the Java sense of the word. It serializes requests, which ensures that access to unsynchronized variables can't cause problems (because it ensures that there is only a single thread ever accessing them at any one time).

Implementing STM does not make servlets thread-safe; there are other sources of concurrency problems (e.g. if mutable state is shared in sessions or the servlet context). That is one of the reasons it has been deprecated.

The correct way to avoid concurrency issues is to properly guard access to shared mutable state by using synchronization, atomic primitives, ThreadLocal etc.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Implementing STM does not make servlets thread-safe


It�s indeed a bad idea to use SingleThreadModel. STM ensures that each servlet handles only one request at a time. The container may implement that behavior in several different container dependent ways. One strategy is to create a pool of servlet instances. Since only one thread is executing in a given servlet instance, this strategy appears "thread-safe". However, it gives a false impression about thread-safety as static or class variables are never thread safe. Static variables are not associated to a particular instance, and there exists only a single copy of such variables. No matter how many instances are created, it�s the same copy which is shared across all instances. Secondly, instance variables cannot be used to share data among multiple requests because the instances serving each request might be different. Thirdly, single-thread model approach doesn�t guard attributes stored in session and context scopes, they are still being shared between multiple instances.

Regards,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic