Reputation: 23229
I have a situation where I would like a method peekWait
on BlockingQueue
. I would describe this method as
retrieves but not remove the head of the queue, waiting if necessary until an element becomes available
I cannot see a corresponding method on BlockingQueue, LinkedBlockingQueue. Does this exist or can I do this somehow? I considered doing poll()
+ addFirst()
but the queue may fill up in between leaving me stuck.
Upvotes: 4
Views: 2196
Reputation: 64632
Wrap the call to BlockingQueue#peek()
in a Callable
, execute it and wait on Future<T>.get(long, TimeUnit)
:
final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);
ExecutorService executor = Executors.newCachedThreadPool();
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3);
scheduler.schedule(new Runnable()
{
@Override
public void run()
{
String e = UUID.randomUUID().toString();
System.out.println("adding = " + e);
queue.add(e);
}
}, 2, TimeUnit.SECONDS);
Callable<String> task = new Callable<String>()
{
@Override
public String call() throws Exception
{
String result = null;
while ((result = queue.peek()) == null)
{
Thread.sleep(100L);
}
return result;
}
};
String peeked = null;
try
{
peeked = executor.submit(task).get(1, TimeUnit.SECONDS);
System.out.println("this should never be printed");
queue.poll();
}
catch (TimeoutException e)
{
System.out.println("null: peeked = " + peeked);
e.printStackTrace();
}
try
{
peeked = executor.submit(task).get(2, TimeUnit.SECONDS);
System.out.println("not null: peeked = " + peeked);
}
catch (TimeoutException e)
{
e.printStackTrace();
System.out.println("should not throw an exception");
}
Upvotes: 1