Java 如何中断阻塞队列?
BlockingQueue.put可能引发InterruptedException。 如何通过抛出此异常来中断队列Java 如何中断阻塞队列?,java,android,concurrency,blocking,interrupt,Java,Android,Concurrency,Blocking,Interrupt,BlockingQueue.put可能引发InterruptedException。 如何通过抛出此异常来中断队列 ArrayBlockingQueue<Param> queue = new ArrayBlockingQueue<Param>(NUMBER_OF_MEMBERS); ... try { queue.put(param); } catch (InterruptedException e) { Log.w(TAG, "put Interrupt
ArrayBlockingQueue<Param> queue = new ArrayBlockingQueue<Param>(NUMBER_OF_MEMBERS);
...
try {
queue.put(param);
} catch (InterruptedException e) {
Log.w(TAG, "put Interrupted", e);
}
...
// how can I queue.notify?
ArrayBlockingQueue=新的ArrayBlockingQueue(成员的数量);
...
试一试{
queue.put(param);
}捕捉(中断异常e){
Log.w(标签“放置中断”,e);
}
...
//如何排队通知?
您需要中断调用队列的线程。put(…)代码>。put(…)代码>调用在某些内部条件下执行wait()
,如果调用put(…)
的线程被中断,wait(…)
调用将抛出InterruptedException
,该异常由put(…)传递代码>
要获取线程,您可以在创建线程时存储它:
Thread workerThread = new Thread(myRunnable);
...
workerThread.interrupt();
或者,您可以使用Thread.currentThread()
方法调用并将其存储在某个地方,供其他人用于中断
public class MyRunnable implements Runnable {
public Thread myThread;
public void run() {
myThread = Thread.currentThread();
...
}
public void interruptMe() {
myThread.interrupt();
}
}
最后,当捕捉到InterruptedException
时,立即重新中断线程是一种很好的模式,因为当抛出InterruptedException
时,线程上的中断状态被清除
try {
queue.put(param);
} catch (InterruptedException e) {
// immediately re-interrupt the thread
Thread.currentThread().interrupt();
Log.w(TAG, "put Interrupted", e);
// maybe we should stop the thread here
}
调用put
将等待插槽空闲,然后再添加param
,流可以继续
如果在调用put
时捕获正在运行的线程(即在调用put
之前调用thread t1=thread.currentThread()
),然后在另一个线程中调用interrupt
(同时t1
被阻止)
具有类似的功能,它负责在给定超时后调用中断。您需要使用queue.put()引用运行代码的线程,如本测试中所示
Thread t = new Thread() {
public void run() {
BlockingQueue queue = new ArrayBlockingQueue(1);
try {
queue.put(new Object());
queue.put(new Object());
} catch (InterruptedException e) {
e.printStackTrace();
}
};
};
t.start();
Thread.sleep(100);
t.interrupt();
Thread t = new Thread() {
public void run() {
BlockingQueue queue = new ArrayBlockingQueue(1);
try {
queue.put(new Object());
queue.put(new Object());
} catch (InterruptedException e) {
e.printStackTrace();
}
};
};
t.start();
Thread.sleep(100);
t.interrupt();