[concurrency-interest] sharing a collection

Peter Kovacs peter.kovacs.1.0rc at gmail.com
Sat Jan 13 19:59:29 EST 2007


On 1/13/07, Tim Peierls <tim at peierls.net> wrote:
> On 1/13/07, First Last <chancer357 at hotmail.com> wrote:
> > I have a Runnable that has a bunch of un-synchronized state, and I place
> > that into an Executor
> > The task will do some calculations and update the state, this should be ok
> > because only 1 thread has it.
> > Then the task will go and resubmit itself to the executor
> >
> >
> >
> > class State {
> >   int x = ..;
> >   List<?> y = ...;
> > }
> >
> > final ExecutorService executor = ...;
> >
> > Runnable r = new Runnable() {
> >   State s = new State();
> >   public void run() {
> >
> >     // add things to s.y
> >     // change s.x
> >
> >     executor.execute(this);
> >
> >   }
> >
> > Since it now may get picked up by another thread, will the other thread
> > correctly see changes to that state?
> >
>
> Yes, it will. From
> http://java.sun.com/javase/6/docs/api/java/util/concurrent/package-summary.html
> :
>
> "Actions in a thread prior to the submission of a Runnable to an Executor
> happen-before its execution begins."

I wonder if the quoted guarantee really applies to the case described
by the original poster without further qualification. For example, I
see no "guarantees" in that particular Runnable implementation as to
the actual place of the submission itself in the order of events. The
time at which the submission itself actually happens is crucial in the
context of this guarantee. If the repeated submission happens before
changes to "s" or between two distinct changes to "s", the quoted
guarantee will result in changes not being seen by the new thread or
not in the order as they appear in the code.

BTW, abstracting away from the example discussed: how an Executor
knows that a Runnable submitted to it has been running in another
thread? That knowledge would be a precondition for the enforceability
of this guarantee.

Peter

>
> (The word "its" refers to the Runnable, not the Thread, though this isn't
> clear out of context.)
>
> --tim
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
>


More information about the Concurrency-interest mailing list