[concurrency-interest] Latches and safe handoff

Dimitris Andreou jim.andreou at gmail.com
Tue Dec 15 19:13:46 EST 2009


Hi Dhanji,

If you are talking about a j.u.c.CountDownLatch or friends, then it's
safe. Quoting javadocs:

Memory consistency effects: Actions in a thread prior to calling
countDown() happen-before actions following a successful return from a
corresponding await() in another thread.

Dimitris

2009/12/16 Dhanji R. Prasanna <dhanji at gmail.com>:
> Here is a pattern that I am curious about:
> public void doStuff() {
>     MyException[] holder = new MyException[1];
>     otherThreadPerformer.perform(new Runnable() {
>         ... run() {
>            try {
>            ...
>            } catch (MyException e) {
>               holder[0] = e;
>            } finally {
>               latch.go(); //can't recall what the method is called
>            }
>        }
>    }
>    latch.await();
>    // read holder[0]
> }
>
> Is this handoff safe? Or am I better off using an AtomicReference? I imagine
> that it is not, as the holder array could still be null (there is no
> synchronized write to it) after the latch is opened.
> Dhanji.
> _______________________________________________
> 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