什么情况下BlockingQueue.take会抛出中断异常?
假设我有一个线程消耗另一个线程生成的项目。它的run方法如下,inQueue是一个BlockingQueue,
boolean shutdown = false;
while (!shutdown) {
try {
WorkItem w = inQueue.take();
w.consume();
} catch (InterruptedException e) {
shutdown = true;
}
}
此外,另一个线程会通过中断这个正在运行的线程来发出没有更多工作项的信号。如果 take() 不需要阻塞来检索下一个工作项,则会抛出中断异常。即,如果生产者发出信号表示已完成填充工作队列,是否有可能意外地将某些项目留在 inQueue 中或错过中断?
Let us suppose that I have a thread that consumes items produced by another thread. Its run method is as follows, with inQueue being a BlockingQueue
boolean shutdown = false;
while (!shutdown) {
try {
WorkItem w = inQueue.take();
w.consume();
} catch (InterruptedException e) {
shutdown = true;
}
}
Furthermore, a different thread will signal that there are no more work items by interrupting this running thread. Will take() throw an interrupted exception if it does not need to block to retrieve the next work item. i.e. if the producer signals that it is done filling the work queue, is it possible to accidentally leave some items in inQueue or miss the interrupt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
发出阻塞队列终止信号的一个好方法是向队列提交一个“poison”值,表明已发生关闭。这确保了队列的预期行为得到遵守。如果您关心清除队列,调用 Thread.interupt() 可能不是一个好主意。
提供一些代码:
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:
我想知道同样的事情并阅读 take() 我相信只有在取出队列中的所有项目后才会抛出中断异常,因为如果队列有项目,它就不会必须“等待”。
但我做了一个小测试:
跑步者将获取 10-11 个项目,然后完成,即 take() 将抛出 InterruptedException,即使队列中仍然有项目。
摘要: 使用毒丸方法,然后您就可以完全控制队列中剩余的数量。
I wondered about the same thing and reading the javadoc for take() I believed that it would throw an interrupted exception only after having taken all the items in the queue, since if the queue had items, it would not have to "wait".
But I made a small test:
The runner will take 10-11 items and then finish i.e. take() will throw InterruptedException even if there still is items in the queue.
Summary: Use the Poison pill approach instead, then you have full control over how much is left in the queue.
根据 javadoc< /a>,如果在等待期间被中断,
take()
方法将抛出InterruptedException
。According to javadoc, the
take()
method will throwInterruptedException
if interrupted while waiting.如果您使用
ExecutorService::execute(Runnable)
启动线程,则通常无法从外部代码中断ExecutorService
的线程,因为外部代码没有对每个正在运行的线程的 Thread 对象的引用(如果您需要 ExecutorService::execute ,请参阅此答案的末尾以获取解决方案)。但是,如果您改为使用ExecutorService::submit(Callable)
提交作业,则会返回一个Future
,它在内部保留对一旦Callable::call()
开始执行,正在运行的线程。该线程可以通过调用Future::cancel(true)
来中断。因此,Callable
内(或由其调用)检查当前线程中断状态的任何代码都可以通过Future
引用来中断。这包括BlockingQueue::take()
,即使被阻塞,它也会响应线程中断。 (如果在阻塞时被中断,JRE 阻塞方法通常会醒来,意识到它们已被中断,并抛出InterruptedException
。)总结一下:
Future::cancel()
和 < code>Future::cancel(true) 都取消未来的工作,而Future::cancel(true)
也中断正在进行的工作 >(只要正在进行的工作响应线程中断)。两个cancel
调用都不会影响已成功完成的工作。请注意,一旦线程因取消而中断,就会在线程内引发
InterruptException
(例如,在本例中通过BlockingQueue::take()
)。但是,下次您在成功取消的Future
上调用Future::get()
时,将在主线程中抛出CancellationException
异常。 (即在完成之前被取消的Future
)。这与您通常期望的不同:如果未取消的Callable
抛出InterruptedException
,则下一次调用Future::get()
将抛出InterruptedException
,但如果取消的Callable
抛出InterruptedException
,则下一次调用Future::get()
将通过CancellationException
。下面是一个说明这一点的示例:
每次运行此命令时,输出都会不同,但这是一次运行:
这表明线程 2 和线程 3 在调用
Future::cancel()
之前完成。线程 1 被取消,因此在内部抛出了 InterruptedException,在外部抛出了 CancellationException。线程 0 在开始运行之前被取消。 (请注意,线程索引通常不会与Future
索引相关,因此Future 0 被取消
可能对应于线程 0 或线程 1 被取消,并且Future 1 被取消
也是如此。)高级:使用
Executor::execute
实现相同效果的一种方法(不返回 < code>Future 参考)而不是Executor::submit
将使用自定义ThreadFactory
创建一个ThreadPoolExecutor
,并让您的ThreadFactory
在并发集合(例如并发队列)中为创建的每个线程记录一个引用。然后,要取消所有线程,您只需对所有先前创建的线程调用 Thread::interrupt() 即可。但是,您需要处理竞争条件,即在中断现有线程时可能会创建新线程。要处理此问题,请设置一个对ThreadFactory
可见的AtomicBoolean
标志,告诉它不要创建更多线程,然后在设置后取消现有线程。You can't in general interrupt the threads of an
ExecutorService
from external code if you usedExecutorService::execute(Runnable)
to start the threads, because external code does not have a reference to theThread
objects of each of the running threads (see the end of this answer for a solution though, if you needExecutorService::execute
). However, if you instead useExecutorService::submit(Callable<T>)
to submit the jobs, you get back aFuture<T>
, which internally keeps a reference to the running thread onceCallable::call()
begins execution. This thread can be interrupted by callingFuture::cancel(true)
. Any code within (or called by) theCallable
that checks the current thread's interrupt status can therefore be interrupted via theFuture
reference. This includesBlockingQueue::take()
, which, even when blocked, will respond to thread interruption. (JRE blocking methods will typically wake up if interrupted while blocked, realize they have been interrupted, and throw anInterruptedException
.)To summarize:
Future::cancel()
andFuture::cancel(true)
both cancel future work, whileFuture::cancel(true)
also interrupts ongoing work (as long as the ongoing work responds to thread interrupt). Neither of the twocancel
invocations affects work that has already successfully completed.Note that once a thread is interrupted by cancellation, an
InterruptException
will be thrown within the thread (e.g. byBlockingQueue::take()
in this case). However, you aCancellationException
will be thrown back in the main thread the next time you callFuture::get()
on a successfully cancelledFuture
(i.e. aFuture
that was cancelled before it completed). This is different from what you would normally expect: if a non-cancelledCallable
throwsInterruptedException
, the next call toFuture::get()
will throwInterruptedException
, but if a cancelledCallable
throwsInterruptedException
, the next call toFuture::get()
will throughCancellationException
.Here's an example that illustrates this:
Each time you run this, the output will be different, but here's one run:
This shows that Thread 2 and Thread 3 completed before
Future::cancel()
was called. Thread 1 was cancelled, so internallyInterruptedException
was thrown, and externallyCancellationException
was thrown. Thread 0 was cancelled before it started running. (Note that the thread indices won't in general correlate with theFuture
indices, soFuture 0 was cancelled
could correspond to either thread 0 or thread 1 being cancelled, and the same forFuture 1 was cancelled
.)Advanced: one way to achieve the same effect with
Executor::execute
(which does not return aFuture
reference) rather thanExecutor::submit
would be to create aThreadPoolExecutor
with a customThreadFactory
, and have yourThreadFactory
record a reference in a concurrent collection (e.g. a concurrent queue) for every thread created. Then to cancel all threads, you can simply callThread::interrupt()
on all previously-created threads. However, you will need to deal with the race condition that new threads may be created while you are interrupting existing threads. To handle this, set anAtomicBoolean
flag, visible to theThreadFactory
, that tells it not to create any more threads, then once that is set, cancel the existing threads.java.concurrency.utils 包是由并发编程领域的一些最优秀的人才设计和实现的。此外,他们的书“Java Concurrency in Practice”明确认可将中断线程作为终止线程的方法。因此,如果由于中断而导致任何项目留在队列中,我会感到非常惊讶。
The java.concurrency.utils package was designed and implemented by some of the finest minds in concurrent programming. Also, interrupting threads as a means to terminate them is explicitly endorsed by their book "Java Concurrency in Practice". Therefore, I would be extremely surprised if any items were left in the queue due to an interrupt.