• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is this thread safe ?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a "MyServlet.java" servlet, I instantiated an action class there and in the Action class there are class variables. Are those variables thread safe ? please see code below ---
**************************************
public class MyServlet extends HttpServlet {
public void doGet(...) { .... }
public void doSomthing(..) {
Action action = new Action();
action.doIt(request, response);
}
}
public class Action {
private String name;

public void doIt(HttpServletRequest req, HttpServletResponse res) {
name = req.getParamter("NAME") + "abc";
.... // perform some business using the request, response obj
}
}
******************************
Is variable "name" thread safe ?
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"flying jordan",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp.
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
Please be sure to change your name. Accounts with invalid names get deleted!
Thanks.
Dave
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Action object is a local object of doSomething(). This object's state will be thread-safe. BTW you have defined "name" as an instance, not a class variable.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While it's true that the 'name' variable is thread-safe as written, if it were actually coded as a class variable as you described, it would not be; any access to a class variable would have to be made thread-safe.
 
flying jordan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ken, "name" is an instance variable for "Action". But, since the "action" instance is a local varible in Servlet so it is thread safe, i.e. no two thread can access the same "action" object at the same time, then this "action" instance belongs to one thread only. So from client point of view, any instance varible of this "action" instance is thread safe too. If "name" is an instance variable of the servlet then it is not thread safe.
Do you think this makes sense ?
thanks.
 
Ken Pelletier
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. You'd originally described the variable as a class variable, but your code declared it as an instance variable.
I was pointing out that, if it was indeed intended to be a class variable, as your description stated, then it would *not* be thread-safe.
It wasn't obvious whether your description was wrong or the code was wrong since they contradicted each other.
As it's shown in the *code*, the reference to the action instance is indeed a local variable ( on the stack, not the heap ) to the service method of the servlet, and you can be sure that only that one thread will have access to it; it's state (ie: the 'name' instance variable) *is* thread-saf, therefore.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"flying jordan"
Change your name
If you don't your account will be deleted.
Unfortunately this will be your last warning.
thanks,
Dave
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic