• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Doubt in Thread Safe Servlet.

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 250
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic