• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread-safety

 
aliasger talib
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
Just wanted to know that does the keyword "final" before an attribute make it thread-safe automatically?
Eg:
public class Test extends HttpServlet {
private static final TestVar var = new TestVar(5000);
}
(Assuming TestVar object exists)
Is the var attribute here thread-safe?
Thanks for your time.
- A
 
Rishi Yagnik
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nop
only request and local var are thread safe and
instance variables if servlet implements SingleThread Model
class var and shared objects like Servlet context and session are not thread safe.
and as yr Object is static it is not thread safe
i hope my ans will work for u
Rishi
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by aliasger talib:
Hi there,
Just wanted to know that does the keyword "final" before an attribute make it thread-safe automatically?
Eg:
public class Test extends HttpServlet {
private static final TestVar var = new TestVar(5000);
}
(Assuming TestVar object exists)
Is the var attribute here thread-safe?
Thanks for your time.
- A


This is not thread safe as it is object reference only and object's value can be changed.
But if it is like
public class Test extends HttpServlet {
private static final int dataType = 10;
}
then it is a thread safe as its value cant be changd.
CMIW
HTH
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slightly disagree with Ravish.
It would be threadsave if the Member-Variables of class TestVar are not mutable from the servlet.
We don't know this from question!!!
example for its_threadsafe:
public class TestVar {
private int variable;
TestVar(int i) {
variable = i;
}
}
If the variable would have public access modifier or if it would have public accessor methods, it wouldn't be thread safe, because as explained by Ravish.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic