wood burning stoves 2.0*
The moose likes Servlets and the fly likes Is this thread safe ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is this thread safe ?" Watch "Is this thread safe ?" New topic
Author

Is this thread safe ?

flying jordan
Greenhorn

Joined: Aug 02, 2002
Posts: 22
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 ?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"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
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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.
Ken Pelletier
Ranch Hand

Joined: Aug 01, 2002
Posts: 54
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

Joined: Aug 02, 2002
Posts: 22
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

Joined: Aug 01, 2002
Posts: 54
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

Joined: Mar 06, 2001
Posts: 13459

"flying jordan"
Change your name
If you don't your account will be deleted.
Unfortunately this will be your last warning.
thanks,
Dave
 
jQuery in Action, 2nd edition
 
subject: Is this thread safe ?
 
Similar Threads
Thread and Heap Memory
Is static inner class thread safe?
Is static inner class thread safe?
request atributes are always thread-safe?
Is static inner class thread safe?