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

David Holmes davidcholmes at aapt.net.au
Tue Mar 20 20:15:22 EDT 2012


andrew.gomilko at gmail.com writes:
>
> Probably, my question is silly.
> But, Is the data field being set in the constructor, doesn't
> imply it's visibility to     the class members?

No, not in general. If an object is constructed and then published for use
across multiple threads, then for the constructed fields to be properly
visible a "safe publication" mechanism must be used. Here fork() is a safe
means of sharing the FJT across threads.

David Holmes

> - Andrew
>
> On Mar 20, 2012, at 2:31 AM, Aleksey Shipilev
> <aleksey.shipilev at gmail.com> wrote:
>
> > 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.
> > _______________________________________________
> > Concurrency-interest mailing list
> > Concurrency-interest at cs.oswego.edu
> > http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>



More information about the Concurrency-interest mailing list