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

Hanson Char hanson.char at gmail.com
Tue Apr 24 23:38:38 EDT 2007


In your test case, since both blocking queues are pre-inserted with items,
it is basically a simulation of the case when messages arrive simultaneously
to both queues.  The behavior of which got dequeued first and whether both
would got dequeued, if I understand it correctly, depends on the thread
scheduling policy of the thread pool executor, which in turns depend on the
things like the number of CPU's on which the JVM is run.  It is as
deterministic as how these things pan out.

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

Cheers,
Hanson Char

On 4/24/07, Szabolcs Ferenczi <szabolcs.ferenczi at gmail.com> wrote:
>
> On 24/04/07, Hanson Char <hanson.char at gmail.com> wrote:
> > See DoubleQueueAccess.java below.  Note conceivably two new messages can
> > arrive at both queues/mailboxes simultaneously and both gets
> dequeued.  In
> > such case the result (in the form of AtomicReferenceArray) will contain
> both
> > messages.  Otherwise, only one of the two array elements contains a
> non-null
> > message.
>
> Hi Hanson,
>     it is an interesting construction. However, it behaves a bit
> different to what you claim. If I prepare a message in both queues and
> call dequeue() on DoubleQueueAccess instance, it seems it is quite non
> deterministically returns one, the other or both. So the test below
> sometimes pass, sometimes fail.
>
> Best Regards,
> Szabolcs
>
> public class DoubleQueueAccessTest {
>
>     DoubleQueueAccess<Integer> dq;
>     BlockingQueue<Integer> q1;
>     BlockingQueue<Integer> q2;
>     final Integer token = new Integer(7);
>     final Integer token1 = new Integer(8);
>
>     @Before
>     public void setUp() {
>         q1 = new LinkedBlockingQueue<Integer>();
>         q2 = new LinkedBlockingQueue<Integer>();
>         dq = new DoubleQueueAccess<Integer>(q1, q2);
>     }
>
>     @Test
>     public void seqAddQ1Q2() throws Throwable {
>         q1.add(token);
>         q2.add(token1);
>         AtomicReferenceArray<Integer> ret = dq.dequeue();
>         assertEquals(token, ret.get(0));
>         assertEquals(token1, ret.get(1));
>     }
> }
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20070424/f2718c71/attachment.html 


More information about the Concurrency-interest mailing list