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

Szabolcs Ferenczi szabolcs.ferenczi at gmail.com
Tue Apr 24 15:26:31 EDT 2007


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));
    }
}


More information about the Concurrency-interest mailing list