您的位置:首页 > 产品设计 > UI/UE

Under what conditions will BlockingQueue.take throw interrupted exception?

2015-03-27 13:52 288 查看
Poison Pill:A good way to signal termination of a blocking queue is to submit a 'poison' value into the queue that
indicates a shutdown has occurred. This ensures that the expected behavior of the queue is honored. Calling Thread.interupt() is probably not a good idea if you care about clearing the queue.

To provide some code:
boolean shutdown = false;while (!shutdown) {
try {
WorkItem w = inQueue.take();
if (w == QUEUE_IS_DEAD)
shutdown = true;
else
w.consume();
} catch (InterruptedException e) {
// possibly submit QUEUE_IS_DEAD to the queue
}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐