[concurrency-interest] ForkJoinTask.fork() enforces visibility?

Aleksey Shipilev aleksey.shipilev at gmail.com
Tue Mar 20 05:31:52 EDT 2012


Hi,

I'm looking over ForkJoinTask.fork() JavaDoc, and reading this:

     * Arranges to asynchronously execute this task.  While it is not
     * necessarily enforced, it is a usage error to fork a task more
     * than once unless it has completed and been reinitialized.
     * 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 {@link #join} or related methods, or a call to {@link
     * #isDone} returning {@code true}.

...is that implying that the data passed in ForkJoinTask.fork() *is*
visible (i.e. fork() enforces visibility)?

To be more precise:

public void submit() {
     Data data1 = new Data();
     new MyForkJoinTask(data1).fork(); // fork, submitting with data1
}

class MyForkJoinTask<Void> {

    private Data data;  // note this is not final nor volatile

    public class MyForkJoinTask(Data data) {
        this.data = data;
    }

    @Override
    public final boolean exec() {
        // is $data guaranteed to be visible as "data1" here?
    }

    ...
}

I had looked over FJP javadoc and saw no other visibility guarantees
there. I remember Brian Goetz' article describing FJP is guaranteeing
visibility in cases like this. It would be perfect to reflect that in
javadocs.

-Aleksey.


More information about the Concurrency-interest mailing list