Reputation: 69997
Is there a way to safely populate a byte array from multiple threads (e.g. first thread fills the first half, the second thread fills the second half using System.arraycopy) without synchronizing on the array itself using Java 6 or 7? The jsr166 related libraries only contain int arrays (AtomicIntegerArray, ParallelIntegerArray).
Upvotes: 1
Views: 2345
Reputation: 13468
Oh this sounds like a good way to get a headache. :) I think I would go for one array per thread, and later join them.
Upvotes: 0
Reputation: 147154
Yes it works. Writing to an array location does not interfere with nearby locations. However, you need to make sure that all threads have finished before reading (a happens-before relationship). The fact that you are using arrays makes no difference.
Upvotes: 6