my dog learned polymorphism*
The moose likes Servlets and the fly likes Thread-safety question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread-safety question" Watch "Thread-safety question" New topic
Author

Thread-safety question

aliasger talib
Ranch Hand

Joined: Aug 19, 2002
Posts: 37
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 thread-safe here?
Thanks so much.
- A
Dominic Paquette
Ranch Hand

Joined: Dec 13, 2002
Posts: 64
Hi,
I don't think it makes it thread safe. In your code the final modifier will only ensure that your variable is refenrencing the same object. But the object can be modified by 2 threads at the same time.
Dominic
Arun Boraiah
Ranch Hand

Joined: Nov 28, 2001
Posts: 233
Since it is a servlet instance variable, it need not be declared as static.
Can you be more specific about the concern of thread safety you are referring? I guess the following link regarding javax.servlet.SingleThreadModel may help you.
http://www16.brinkster.com/saifpia/servlets/single.html


Sharing is learning
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Arrrggghhh.... No, final does not make it thread-safe. The best way to make your WHOLE servlet thread-safe is to NOT use any instance variables -- what were you planning on putting in that variable, anyway?
Kyle
BTW, do a search here on Javaranch and you'll find this question is answered quite completely every two weeks or so...


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
aliasger talib
Ranch Hand

Joined: Aug 19, 2002
Posts: 37
Thanks guys. I guess I'm still in the learning phase.
I was going thru some thread safety material and this just popped into my head.
JavaRanch - best place to solve queries I'd say!
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

thread-safety issues are all about one thread making changes whilst another thread expects things to stay the same.

So in a way, making something 'final' is a way to make it 'thread - safe'... but only as a side-effect. The primary meaning behind a final variable is that its value will not (and cannot) change.

For example, if you wanted to represent a set of options, you MIGHT do something like:
public static final int RED = 1;
public static final int BLUE = 2;

In this way, RED will forever by represented by an integer value of 1.

And is it "thread-safe" - yes.. but only because it doesn't make sense to worry about changing something that is not changeable.

Did that make sense?

FYI: My favourite "thread-safe" thread (hehe) is one made by simon brown (I think) some time back, which i've just spent half a frustrating hour trying to find.

Oh well...
aliasger talib
Ranch Hand

Joined: Aug 19, 2002
Posts: 37
ok cool.
so what we're saying here, is that the final keyword will not _necessairly_ make the attribute thread safe?
So it's better to assume that it's not thread safe?
I hope I'm on the right page with you here. :-(
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I might have led you down a wrong turn by talking about final and thread-safe in the same sentence. I meant to say only that final variables (and by that, I should say primitive variables) are thread-safe as a by-product of being final.

the 'final' keyword is just NOT an appropriate keyword to think about when you are thinking of making your code "thread safe".

the 'synchronize' keyword is (combined with general avoidance of non-local variables in your servlet) .

I wish I could find that original post I mentioned, I've asked in our Moderator's forum for some help locating it. Meanwhile, check out this thread for a different discussion of thread -safety.

Please read only the original question, and the posts by William Brogden and Michael Ernest. These 3 posts are the most "on topic", and other posts are simply not correct.

http://www.coderanch.com/t/355515/Servlets/java/instance-variables-thread-safe
[ January 21, 2003: Message edited by: Mike Curwen ]
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Pauline McNamara found the thread you were looking for (where I blew a gasket over threadsafety). It's this thread.
Kyle
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Thank-you Kyle and Pauline.

What made me think of that post was that someone else was confusing a threading issue and the 'solution' for it. Bottom line....

Thread issues and their solutions revolve around well-designed code and the synchronize keyword. Not static, not final.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread-safety question
 
Similar Threads
Thread Safe Instance Variables
Struts 2: %{} or #
single-threaded
c:set tag not clear
Thread-safety