• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Arrays and thread safety

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have the following class:



If array were an int, the above would be threadsafe. If it were an object, it would depend on the object. But what about an array of ints ? I'm thinking about what happens when an array gets allocated.

Thanks,

Michael
 
Michael Farragher
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In respect of the above,add array[0] = 11 to the constructor
 
Sheriff
Posts: 16669
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, if it were just an int it would be thread safe because there's no way to change the value. If it were some type of object, then the object would have to be immutable for it to be thread safe. An int[] is an object and it's mutable because the getArray() method returns a direct reference to it. Therefore, this class is not thread safe. To make it safe, you'd have to return a copy of the array instead.
 
Ranch Hand
Posts: 164
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu, even an integer is not guaranteed to be thread safe. That's why we have AtomicInteger.
 
Master Rancher
Posts: 4062
56
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A final int would be thread-safe, yes.  I assume that's what was meant here, since the field in question is final.  A nonfinal int would not be thread-safe.
 
Saloon Keeper
Posts: 13396
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Salil Wadnerkar wrote:Junilu, even an integer is not guaranteed to be thread safe. That's why we have AtomicInteger.


Wrong. Atomicity and thread-safety are related but separate concepts. Using atomic primitives doesn't necessarily make something thread-safe, and something that's thread-safe doesn't necessarily use atomic primitives.

Whether something is thread-safe is rather subjective. It depends on the responsibilities of the class. In the original code in the first post, class A could be considered thread-safe if it assumes responsibility for the array reference, but not the contents of the array. The class' responsibilities should be well documented:
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends on what you mean by thread safety. If you ask about concurrent reading of elements without writing, then yes - it is thread safe. If you plan to change elements, then does not matter if your elements are of int or any other primitive or object type - your code will not be thread safe. Guess what? Even if you have an array of AtomicIntegers but instead of using methods of AtomicInteger, you do direct assignments of new AtomicInteger instances into elements, then your code is not thread safe again.
 
Marshal
Posts: 74376
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TB: welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 74376
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have thought about this for several hours; I think TB is correct. Even if an assignment numbers[0] = 123; is an atomic operation, if that happens in an un‑synchronized method or without a Lock, that method will behave as thread‑unsafe.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic