This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Is this thread safe? Simple question. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Is this thread safe? Simple question." Watch "Is this thread safe? Simple question." New topic
Author

Is this thread safe? Simple question.

Bryan Lee
Greenhorn

Joined: Feb 13, 2004
Posts: 17
Hi,
Just wondering if this is thread safe:


I know I can have a synchronized method like this to ensure thread safety:


Just curious whether a static variable can ensure the thread safety?


Bryan Lee<br /> <br />SCEA - Sun Certified Enterprise Architect for J2EE Platoform<br />... and couple other certifications.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
If the field is private, no other object can get at it, so it's safe. Making it static makes it thread-unsafe, because other objects of the same class can get at it via aThread.aNumber.


Android appsImageJ pluginsJava web charts
Bryan Lee
Greenhorn

Joined: Feb 13, 2004
Posts: 17
Anyone can recommend me a tutorial in multi-threading, thread-safety...etc. areas?
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
If the field is private, no other object can get at it, so it's safe.

That's not quite right. The private field can be (and most likely is) modified from a public method (or from a private method that is called from a public method).

Making it static makes it thread-unsafe, because other objects of the same class can get at it via aThread.aNumber

That is also not quite right. The fact is, the getNumber() method above is unsafe no matter if the aNumber field is static or not. Here is some code to demonstrate the idea:



The output is likely to be different every time you run this test. Here is what I got from two separate runs:

run1: Expected: 100000000 Actual: 45381942
run2: Expected: 100000000 Actual: 72689195
[ June 30, 2005: Message edited by: John Smith ]
Jack Wiesenthaler
Ranch Hand

Joined: Jul 26, 2001
Posts: 75
If you want to store data on a per thread basis then use ThreadLocal variables ;-)

private static ThreadLocal aNumber = new ThreadLocal();
Jaspreet Singh
Greenhorn

Joined: Jul 07, 2005
Posts: 8
How can you make this code threadsafe, and get an exact value of 100000000 ?


The output is likely to be different every time you run this test. Here is what I got from two separate runs:

run1: Expected: 100000000 Actual: 45381942
run2: Expected: 100000000 Actual: 72689195

[ June 30, 2005: Message edited by: John Smith ][/QB]

Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
How can you make this code threadsafe, and get an exact value of 100000000 ?


You can synchronize the getNumber() method:
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Is this thread safe? Simple question.
 
Similar Threads
StringBuffer thread safe question
Concern about Thread safety
Making Servlet Thread Safe
Thread Safe Code
Regarding synchronization