[concurrency-interest] int[] in the ForkJoin example

Doug Lea dl at cs.oswego.edu
Sat Aug 6 09:43:05 EDT 2011

On 08/05/11 19:43, Ashwin Jayaprakash wrote:
> The Fork-Join example/doc shows an int[] being written to by multiple threads
> (http://download.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html). Each
> task writes to a separate location in the array, but how is it valid without
> being marked as volatile or using AtomicIntegerArray?

As mentioned in the ForkJoinTask documentation,
it is only valid if the tasks each access distinct locations
before joining. See in particular the javadoc for "fork()", saying:

"Subsequent modifications to the state of this task or any data it operates on 
are not necessarily consistently observable by any thread other than the one 
executing it unless preceded by a call to join()  or related methods, or a call 
to isDone()  returning true. "

Internally, the guarantees about ordering and visibility upon
joins (as well as related methods like invoke) are arranged
via volatile/atomic operations that are similar to the ones
used for similar guranatees upon unlocking locks.


More information about the Concurrency-interest mailing list