[concurrency-interest] Latches and safe handoff

Tim Peierls tim at peierls.net
Tue Dec 15 19:20:07 EST 2009


It is safe for the obvious otherThreadPerformer suspects, i.e., something
involving Thread.start/join, Executor.execute, or things like that. (You'll
need holder to be final.)

Seems like you're working very hard to avoid Future/Callable, though.

--tim

On Tue, Dec 15, 2009 at 7:00 PM, Dhanji R. Prasanna <dhanji at gmail.com>wrote:

> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20091215/545dff20/attachment.html>


More information about the Concurrency-interest mailing list