File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlets" Watch "servlets" New topic


Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

What is meant by ThreadSafe?
Thanks in Advance. Bye...
Tony Alicea

Joined: Jan 30, 2000
Posts: 3226
The normal servlet behavior is that multiple simultaneous requests are dispatched to multiple threads concurrently accessing the same servlet instance.
If a servlet implements the SingleThreadModel interface (which is a marker interface like Serializable), the system guarantees that there will not be simultaneous access to the same servlet instance.

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD

Joined: Jun 13, 2001
Posts: 6
Default value of "thread safe = true".
servlet or JSP file may be accessed by multiple users..
servlet engine creates a separate thread for each and evry user.
if u don't want your sevlet to be accesed by multiple users at a time,
you have to implement the servlet class with "singlethreadmodel"
interface.This interface does not contain any methods.It is just a flag.
In JSP,You have to set threadsafe as false
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
For more information on writing thread-safe code, check out this section of Sun's java tutorial on synchronizing threads:

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Prabhu
I would like to clarify my doubt with you regarding SingleThreadModel. In case of Servlet class which implements, SingleThreadModel you said that Servlet will not be accessed by two threads at a time. What I know that, In case of SingleThreadModel, we can create a pool of Instances which can be used for managing the client requests. We can also set the number of Instances to be created and one instances can be dedicated to one request. If more request come then , it will be put in Wait state.
My question is that we can create pool of instances which can be used not like one servlet instance. Please clarify.
I agree. Here's the link:
subject: servlets
It's not a secret anymore!