[concurrency-interest] Disposing a BlockingQueue with capacity?
alarmnummer at gmail.com
Sun Mar 4 11:59:12 EST 2007
what you could do is the following;
add a check before an item is put on the queue. when the structure
shuts down, the check doesn';t allow any items being put by throwing
an illegalstateexception for example, or
Now for the waiting threads. When the structure is shut down, just
take all items that are going to be put.
item = queue.poll(1,TimeUnit.SECOND)
This make sure that pending threads (for putting) are allowed to run.
queue's are great structures, but I don't expose them directly in a
lot of situations because the contract they provide can be
You also have to watch out for discarding items, in some situations
you don't want this to happen.
Another solution would be to interrupt all pending threads. But you
need to have access to the pending threads.
On 3/4/07, Oliver Pfeiffer <pfeiffer at tzi.de> wrote:
> How should a BlockingQueue with a fixed capacity be disposed to release all
> waiting submitter threads?
> Assuming we have a black-box service sequentially processing items. The
> items can be submitted to the black-box by 1..n threads in parallel. The box
> uses a LinkedBlockingQueue with a fixed capacity of 10. When the queue
> becomes full there could be more than 10 (e.g. 1000) blocked threads waiting
> to submit further items.
> How can this black-box safely be terminated by releasing all submitter
> threads? Unfortunately the capacity can't be set to infinity after
> construction and a BlockingQueue#clear() does only clear the currently
> queued items. Thus in the example above we will release 10 threads
> (successful submit) but will still have 990 threads blocked.
> Grüße - Regards
> Oliver Pfeiffer
> ICQ-ID 84320006
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
More information about the Concurrency-interest