本文介绍了如何立即释放等待BlockingQueue的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个 BlockingQueue 和几个线程等待 poll(long,TimeUnit) code> take())。



现在队列是空的,希望通知等待线程,等候。预期的行为是返回 null 或声明 InterruptedException



Object.notify()不适用于 LinkedBlockingQueue

解决方案

Javadoc for the BlockingQueue建议一个好方法:


Consider a BlockingQueue and a few threads waiting on poll(long, TimeUnit) (possibly also on on take()).

Now the queue is empty and it is desired to notify the waiting threads that they can stop waiting. The expected behaviour is to have either null returned or the declared InterruptedException thrown.

Object.notify() won't work for LinkedBlockingQueue as the threads are waiting on an internal lock.

Any straightforward way?

解决方案

Javadoc for the BlockingQueue suggests a good way:

这篇关于如何立即释放等待BlockingQueue的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 09:23