[concurrency-interest] sharing a collection

Tim Peierls tim at peierls.net
Sat Jan 13 17:32:01 EST 2007


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."

(The word "its" refers to the Runnable, not the Thread, though this isn't
clear out of context.)

--tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20070113/d40ebf72/attachment.html 


More information about the Concurrency-interest mailing list