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

Joe Bowbeer joe.bowbeer at gmail.com
Mon Apr 23 13:48:11 EDT 2007

I support your intuition to use an Executor.  That gives you a queue
and a pool of threads.  See Executors for a list of prefabbed
configurations.  You'll need to arrange for your producers to package
their events as Runnables so they can be executed by your Executor.

If your mailboxes are prioritized, you can configure your executor to
use a priority queue.

Ignoring Executors for the moment, the basic mechanisms for signaling
threads are Object.notify and Condition.signal.  If you were trying to
build a single-threaded gizmo like you described from scratch, you can
code your thread to await on a Condition, and code your producer or
your queues so that the Condition is signaled whenever an event is
added to either queue.  (If you're using the Object-based mechanism,
then the thread waits on the Object while the producers notify the


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

More information about the Concurrency-interest mailing list