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

Hanson Char hanson.char at gmail.com
Tue Apr 24 13:08:01 EDT 2007


How about the use of ExecutorService.invokeAny with two Callables, each
trying to dequeue from a BlockingQueue (representing the mailboxes) ?

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.

Cheers,
Hanson Char

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReferenceArray;

public class DoubleQueueAccess<T>
{
    private final ExecutorService es = Executors.newCachedThreadPool();
    private final BlockingQueue<T> q1, q2;

    public DoubleQueueAccess(BlockingQueue<T> q1, BlockingQueue<T> q2) {
        this.q1 = q1;
        this.q2 = q2;
    }

    private static class Dequeue<T> implements Callable<T> {
        final BlockingQueue<T> q;
        final AtomicReferenceArray<T> result;
        final int pos;

        Dequeue(BlockingQueue<T> q, AtomicReferenceArray<T> result, int pos)
{
            this.q = q;
            this.result = result;
            this.pos = pos;
        }

        public T call() throws InterruptedException {
            T element = q.take();
            result.set(pos, element);
            return element;
        }
    }

    public AtomicReferenceArray<T> dequeue() throws InterruptedException,
ExecutionException {
        final Collection<Dequeue<T>> callables = new
ArrayList<Dequeue<T>>();
        final AtomicReferenceArray<T> result;
        callables.add(new Dequeue<T>(q1, result = new
AtomicReferenceArray<T>(2), 0));
        callables.add(new Dequeue<T>(q2, result, 1));
        es.invokeAny(callables);
        return result;
    }
}


On 4/23/07, Fred Janon <fjanon at gmail.com> wrote:
>
> Hi,
>
> I used to write real time apps using RSX11M+ (and VMS) that had mailboxes
> and events. There was a "wait for OR(events)". The task would wait for any
> event specified in the OR. Then I would associate an event with a mailbox,
> do that for another mailbox and when a message would arrive in one of the 2
> mailboxes, the task would awake. I am trying to do that in Java with
> threads.
>
> I have been reading "Java Threads",  "JCIP", "Java in a Nutshell" and I
> could think about a couple of classes I could use but I would appreciate
> some design advice... Or if there is an existing library that does that
> already does that, I would appreciate a reference, I did some research and
> did not find anything interesting.
>
> Basically I need 1 public mailbox and 1 private mailbox for a thread. The
> thread would be blocked until a message arrives in one of the mailboxes. The
> mailboxes must have multiple slots. I was thinking about using
> LinkedBlockingQueues as mailboxes since I don't want to block the sender (or
> avoid it) but I am a bit stuck on making the thread wait for a message in
> either queues.
>
> The next step would be to have a ThreadPool for the tasks, sharing a
> common public mailbox to process the messages and each have a private
> mailbox for system management messages. I guess switching to a single thread
> to a ThreadPool would be relatively easy?
>
> Thanks in advance.
>
> Fred
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20070424/dc66e2c0/attachment.html 


More information about the Concurrency-interest mailing list