我有一段类似于这样的代码,它在一个实现Runnable
接口的类的run()
方法中,并且多个该类的实例被启动。
do{
try{
String contractNum=contractNums.take();
}catch(InterruptedException e){
logger.error(e.getMessage(), e);
}
}while(!("*".equals(contractNum)));
contractNums
是一个被多个线程共享的BlockingQueue<String>
。有不同的Runnables
将元素放入此队列。
当捕获到InterruptedException
后,下一步应该做什么我并不确定,是否应该通过重新抛出RuntimeException
(以便我的while
循环终止),或者尝试再次从contractNum queue
中取下一个元素并忽略InterruptedException
?
我不确定是否应将InterruptedException
视为线程终止的致命条件或将其保持在while循环中。
请提供建议。
finally
块中仅调用一次Thread.currentThread.interrupt()
- 无论此异常被抛出的次数如何。在内部catch中,我应该忽略异常并重试从队列中take()
元素。 - Sabir Khan