• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

Is this thread safe? Simple question.

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bryan Lee
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone can recommend me a tutorial in multi-threading, thread-safety...etc. areas?
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to store data on a per thread basis then use ThreadLocal variables ;-)

private static ThreadLocal aNumber = new ThreadLocal();
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]

 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How can you make this code threadsafe, and get an exact value of 100000000 ?



You can synchronize the getNumber() method:
 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic