wood burning stoves*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt in Thread Safe Servlet. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt in Thread Safe Servlet." Watch "Doubt in Thread Safe Servlet." New topic
Author

Doubt in Thread Safe Servlet.

Shanmugam Karthikeyan
Ranch Hand

Joined: Aug 02, 2002
Posts: 58
Hi,

Please calrify my doubt for the below example.

public class MyServlet extends HttpServlet implements SingleThreadModel
{

private String strName;

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {


}

}


In the above example is the instance variable "strName" is thread safe for not?.

In my view "strName" is Thread safe, the reason being that private variables are not shared by mutiple instances.

The reason why iam asking is that i took two mock exams, in one of the mock exam it is not thread safe whereas in another mock exam it has been given as thread safe.

regards

S.Karthikeyan


SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3
Rajesh Vijaya
Ranch Hand

Joined: Oct 18, 2005
Posts: 48
u are rite karthik ....its thread-safe....

but if it is a static private instance variable then its not thread-safe even though it implements SingleThreadModel.


Rajesh <br /> <br />SCJP1.4 SCWCD1.4 SCBCD 1.3 ,SCDJWS(Preparing..)<br /> <br />There is no free will.It is the phenomenon bound by cause and effect.But there is something behind will which is free---Swami Vivekananda...
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

In my view "strName" is Thread safe, the reason being that private variables are not shared by mutiple instances.


Only local/method variables and request attributes are thread safe.

Instance variables/static variables/session scoped attributes/application scoped attributes are NOT thread safe.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Shanmugam Karthikeyan
Ranch Hand

Joined: Aug 02, 2002
Posts: 58
Hi,

Like two different mock exam answers for a single question, here also i have received two different replies for a single question

Iam more confused now.. Can anyone confirm this behaviour.

To make my question very short.

Is the private instance variable declared in a Servlet is Thread Safe or not ?

regards

S.Karthikeyan


regards

S.Karthikeyan
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Private is an access modifier. Which means variables declared with private modifier CAN be accessed by any method(non-static) within the same class.

When 10 users are requesting the same resource 10 threads will be accessing the doGET() method and if you are accessing the private instance variable inside the doGET() method there is a possibility of one thread accessing the value set by another thread which could lead to data corruption.

Thats why it's instance variables are ALWAYS NOT thread safe.
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Vishnu,

But did u notice that the class in the given example implements SingleThreadModel(even though it is deprecated)..which means no multiple threads for a single instance..instead multiple instances will be created based on the request.

Then how do u conclude instance variables in the above class are not thread safe?.Plz explain..


Regards,
Priya.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

But did u notice that the class in the given example implements SingleThreadModel(even though it is deprecated)


Overlooked SingleThreadModel.
Great mistake. Apologies.
Debashish Chakrabarty
Ranch Hand

Joined: May 14, 2002
Posts: 226

The servlet in question implements SingleThreadModel and can be therefore considered thread-safe. The variable "strName" is thread-safe. There is no need to synchronize access to instance variables in Servlets implementing SingleThreadModel.

Having said that here is something one must keep in mind (excerpted from the Servlet spcn):
...SingleThreadModel does not solve all thread safety issues. For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used.

It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources. This interface is deprecated in Servlet API version 2.4.


Debashish
SCJP2, SCWCD 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Thread Safe Servlet.
 
Similar Threads
doubt with the answer
Doubt
Wait() doubt
a doubt
Doubt