• 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
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Safe primitive and an Object Array

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

I was working on a proof of concept for a product, when I ran in to this issue, so I wrote a sample program. To cut to the chase, how do I make this array thread safe ?. Honestly, from the program below, any thread can modify the contents of the array. I was about to explore the possibility of using AtomicIntegerArray / AtomicReferenceArray.

Please advice!.



Best Regards!!..

_SM
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First, you need to define what is thread safe. Is it that when an element is changed, partial writes should not be seen? It it when looping through the array, it can't be changed until the loop is complete? etc. etc.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Rodeo wrote:
Honestly, from the program below, any thread can modify the contents of the array. I was about to explore the possibility of using AtomicIntegerArray / AtomicReferenceArray.



To make something thread safe, you can use synchronization, or you can use the classes you mentioned -- but keep in mind that the atomic classes is for optimistic locking, which is much harder to implement than synchronization. It is not a magic bullet -- that is easier to use than synchronization.

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

Henry,

what I meant was,

int [] qu = new int[20];

even if I do a synchronized(qu) {

}

this won't stop other threads from changing the contents of the array ?. The example I gave you, shows a thread (sleeping for 8 seconds) holding the lock. And another thread modifying the contents of the Array.

So I wanted to know, how I can make it thread safe ( the whole Array ), not just the reference

_SM
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Rodeo wrote:
this won't stop other threads from changing the contents of the array ?. The example I gave you, shows a thread (sleeping for 8 seconds) holding the lock. And another thread modifying the contents of the Array.

So I wanted to know, how I can make it thread safe ( the whole Array ), not just the reference



One requirement of thread safety is cooperation. All the threads must behave together to achieve thread safety. This means that all the threads must agree on how they will behave with the data -- or better yet, the data should be held private, with all public methods that access the data to behave in a cooperative manner.

If you need to operate on the whole array -- and for an operation that is longer than a single read or write -- you can't just give away access to data and expect it to be thread safe.

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

Steven Rodeo wrote:
I was about to explore the possibility of using AtomicIntegerArray / AtomicReferenceArray.


AtomicIntegerArray will not automagically make your array thread safe.
You must still synchronize each access to array elements in your code, but using other technique,
for example like this:

 
reply
    Bookmark Topic Watch Topic
  • New Topic