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

Brian Goetz brian at quiotix.com
Mon Apr 23 12:48:04 EDT 2007

Composing operations on existing blocking operations is difficult;  
one way you can do that is by having an adapter thread that does  
nothing but wait on queue A and move the results to queue T.  If you  
have one adapter thread per source queue (A, B, C), then threads can  
wait on the merged queue T.  But this is ugly, inefficient, and can  
nasty in the edge cases (shutdown, queue full, etc.)  I think you'd  
have to write a "dual headed queue" for the purposes you described  
(which seems entirely doable, as you only need to merge the results  
of two queues.)

Blocking is one of the areas where monitor-based designs do not  
compose well.  The STM guys claim they have the answer.  I hope they do!

On Apr 23, 2007, at 11:29 AM, Peter Veentjer wrote:

> Hi Fred,
> the functionality you are looking for looks a lot like the
> http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/ 
> Selector.html
> in non blocking io (nio). Each mailbox can be seen as an
> http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/ 
> SelectableChannel.html
> and multiple channels can be handled by a single thread.
> 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.
>> 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
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

More information about the Concurrency-interest mailing list