aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread Safe primitive and an Object Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Safe primitive and an Object Array" Watch "Thread Safe primitive and an Object Array" New topic
Author

Thread Safe primitive and an Object Array

Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Mar 06, 2008
Posts: 72

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Safe primitive and an Object Array