Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The SingleThreadModel Interface

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone shed some lights on The SingleThreadModel Interface.
Please....
 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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".
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • 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.
 
Kunal Jag
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic