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

Szabolcs Ferenczi szabolcs.ferenczi at gmail.com
Wed Apr 25 16:23:29 EDT 2007

On 23/04/07, Gregg Wonderly <gregg at cytetech.com> wrote:
> Peter Veentjer wrote:
> > But as far as I know there is no synchronization structure in j.u.c
> > that can provide the same functionality for arbitrary event sources.
> >
> > 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.
> What I would do, is use threads that read from the two queues and put the
> objects into a single queue that you can take from.

Do you mean something like this, or something simpler? At least this
does not introduce non-determinism but handles it:

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Mailboxes<T>
    private BlockingQueue<T> transfer;

    public Mailboxes(BlockingQueue<T> q1, BlockingQueue<T> q2) {
        transfer = new LinkedBlockingQueue<T>();
        Thread c1 = new Copier<T>(q1, transfer);
        Thread c2 = new Copier<T>(q2, transfer);

    private class Copier<T> extends Thread {
        private BlockingQueue<T> inp;
        private BlockingQueue<T> outp;
        public Copier(BlockingQueue<T> from, BlockingQueue<T> to) {
            inp = from;
            outp = to;
        public void run() {
	    try {
	    } catch (Throwable e) {}

    public T wait_for_or() throws Throwable {
        return transfer.take();

More information about the Concurrency-interest mailing list