File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Difference betweeen SingleThreadModel and ThreadSafe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Difference betweeen SingleThreadModel and ThreadSafe" Watch "Difference betweeen SingleThreadModel and ThreadSafe" New topic
Author

Difference betweeen SingleThreadModel and ThreadSafe

Mike Anna
Ranch Hand

Joined: Jul 08, 2007
Posts: 117
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
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  71
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

Joined: Jul 08, 2007
Posts: 117
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

Joined: Jan 10, 2002
Posts: 61769
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike Anna
Ranch Hand

Joined: Jul 08, 2007
Posts: 117
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Mike Anna
Ranch Hand

Joined: Jul 08, 2007
Posts: 117
Thank you everyone for your replies. I now have a better understanding of this subject.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference betweeen SingleThreadModel and ThreadSafe