[concurrency-interest] Blocking queue

Dawid Kurzyniec dawidk at mathcs.emory.edu
Wed May 17 14:06:38 EDT 2006


Grace Kwok wrote:
> Hi,
>
> Conceptually, this is what I'd like to achieve.
> Suppose I have two types of tasks they are put in some
> kind of queue.
>
> I want to have some number of threads desginated to
> block on take() only on type 1 task.
>
> I also want to have some number of threads designated
> to take type 2 task and process.
> However, if none of the type 2 tasks is available,
> then it should take the type 1 task and starts
> processing.
>
> Is there any neat way of doing it besides making two
> queues and poll on them alternatively?
>   

I'd implement it by writing a custom blocking queue implementation (not 
necessarily implementing the BlockingQueue interface) using two 
non-blocking queues internally and providing its own wait/notification 
mechanisms on top of that, using condition variables. For instance, 
type1queueNotEmpty, anyQueueNotEmpty, type1queueNotFull, and 
type2queueNotFull might be your conditions; the put(type2obj) would 
await on type2queueNotFull, then put, then signal anyQueueNotEmpty. Your 
"takeType1task()" would wait on type1queueNotEmpty, then take, then 
signal type1queueNotFull, etc.

Regards,
Dawid



More information about the Concurrency-interest mailing list