[concurrency-interest] BlockingQueue or Queue / peek()

Johannes.Lichtenberger Johannes.Lichtenberger at uni-konstanz.de
Thu Feb 2 14:29:38 EST 2012


On 02/02/2012 08:19 PM, Johannes.Lichtenberger wrote:
> On 02/02/2012 07:41 PM, Johannes.Lichtenberger wrote:
>> Hello,
>>
>> why doesn't the BlockingQueue interface contain a method similar to
>>
>> public E poll(long timeout, TimeUnit unit) throws InterruptedException;?
>>
>> I think it would be great to allow waiting a specified time if nothing
>> is in the Queue. In some circumstances I'd like to peek() a
>> Future-instance instead of using take() or poll(long, TimeUnit) but it's
>> necessary to wait if at the time calling the method nothing is in the queue.
>>
>> Perhaps I missed something? ;-)
>>
>> Ok, seems peek() is defined in the Queue-interface together with an
>> element()-method. Do I have to catch the NoSuchElementException and loop
>> and sleep() until something is available and to give up after some time
>> period or do I really miss something?
> 
> Or maybe "pulling" the top element and push it back, but hm.

Which certainly would involve some form of external synchronization such
that the producers can't add something in the meanwhile, not really
appropriate I think ;-)


More information about the Concurrency-interest mailing list