Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Thread Safe Servlet.

 
Shanmugam Karthikeyan
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rajesh Vijaya
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Shanmugam Karthikeyan
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 231
Firefox Browser Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic