• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Safe primitive and an Object Array

 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • 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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic