[concurrency-interest] Basic question on making a thread waiting for 2 mailboxes...

Szabolcs Ferenczi szabolcs.ferenczi at gmail.com
Wed Apr 25 02:44:40 EDT 2007


On 25/04/07, Hanson Char <hanson.char at gmail.com> wrote:

> The assert statements presume the dequeue must happen in a certain order,
> which therefore would appear to fail sometimes.

Hi Hanson,
   just have a look at your algorithm and you see that the certain
order is established by the algorithm so it is quite correct to
presume, isn't it?

        callables.add(new Dequeue<T>(q1, result = new
AtomicReferenceArray<T>(2), 0));
        callables.add(new Dequeue<T>(q2, result, 1));
        es.invokeAny(callables);

So, you assign the first position in the two element
AtomicReferenceArray to the first queue and the second position to the
second queue. That is what the assert statements in the test follow.
No problem with that. The asserts happen after the dequeue() is
returned, so the AtomicReferenceArray is stable. No race condition
applies at that point.

The test sometimes fails and sometimes passes, however, and it is non
deterministic whether one element is received or two. The test also
ensures that one element is there in each queue at the time the
dequeue() is called. The call, on the other hand, returns one or the
other or both. That is no problem if you are aware of it. So you
cannot rely on that if two messages arrive at the two queues
simultaneously, both are returned because it is not the case.

Best Regards,
Szabolcs


More information about the Concurrency-interest mailing list