Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference betweeen SingleThreadModel and ThreadSafe

 
Mike Anna
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have searched a lot, without too much help.
Can some one please explain me the difference between
SingleThreadModel and ThreadSafe ?
Does threadsafe not really mean singlethreadmodel.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a servlet implements SingleThreadModel, then that's a hint to the servlet container that this servlet is not threadsafe, and thus a servlet instance should not be used by multiple threads concurrently. Unfortunately that's not enough to make a web application thread-safe, so STM is deprecated, and should not be used.

The way to make a web app thread-safe is to program the servlets (and other code) in a thread-safe manner. That means (amongst other things) not sharing mutable state between threads, e.g. by using instance variables in servlets, or only doing so in synchronized blocks and methods.
 
Mike Anna
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
thanks for the reply. I was not specific about servlets being threadsafe.
It was just that I am unable to understand the difference between calling a class as "singlethreadModel" or "threadsafe". Dont both the terms mean the same ?

Rgds
[ October 08, 2007: Message edited by: Mike Anna ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. SingleThreadModel was a means to allow non-thread-safe servlets to run safely by confining them to a single thread. That's not in any way the same thing as being thread-safe; that is, safe to use across threads.

It's like saying that practicing safe driving habits, and hiding the keys to the car, are the same thing.
 
Mike Anna
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. SingleThreadModel was a means to allow non-thread-safe servlets to run safely by confining them to a single thread. That's not in any way the same thing as being thread-safe; that is, safe to use across threads


ooohhkkk.. now I see. STM is just a marker interface, which would probably instantiate seperate instances of a servlet. However thread-safe really means like having a synchronised keyword around the method or code. right ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest first step to having a threadsafe servlet is to avoid the use of instance variables. Declare all of your variables in the doGet or doPost method.

Overuse or improper use of the synchronized keyword can cause bottlenecks in your app. You may, occasionally need to synchronize access to session or context scoped variables but this should be rare.
 
Mike Anna
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for your replies. I now have a better understanding of this subject.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic