[concurrency-interest] Java ForkJoin demo applications

Doug Lea dl at cs.oswego.edu
Thu Dec 8 16:09:37 EST 2011


On 12/08/11 16:01, Dr Heinz M. Kabutz wrote:
> Hi Doug,
>
> in your Fib example:
> http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/test/loops/Fib.java?revision=1.5&view=markup
>
>
> should "number" not be marked as volatile, as it is accessed from multiple threads?
>

No. See The javadocs for some explanation. Pasting from the spec for 
ForkJoinTask.fork:

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.

As far as memory effects go, reading the results of a joined
computation are basically the same as reading data after
reading a volatile "ready" flag. Usually (in both cases)
you do not need to declare the data itself volatile.

-Doug



More information about the Concurrency-interest mailing list