Java - 优雅地退出线程
我有一堆执行计算的线程。它们使用 CyclicBarrier 进行“同步”
。当任何线程的run()
方法完成时,我希望所有其他线程在调用await()
后也退出代码> 下次就在障碍物上。
到目前为止,我尝试过的所有操作要么在 await()
调用处挂起,要么导致屏障损坏。有什么建议吗?
编辑:这是(基本)代码:
public MyClass implements Runnable {
public void run() {
while (true) {
if (someCondition) {
// quit other threads when they call await()
return;
}
barrier.await();
}
}
I got a bunch of threads that perform calculations. They are "synchronized" using a CyclicBarrier
. When any thread's run()
method finishes, I want all other threads to exit as well once they call await()
on the barrier the next time.
So far, everything I've tried either hangs at the await()
calls or results in a broken barrier. Any tips?
EDIT: Here's the (basic) code:
public MyClass implements Runnable {
public void run() {
while (true) {
if (someCondition) {
// quit other threads when they call await()
return;
}
barrier.await();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
reset() 将唤醒所有等待线程并抛出异常,
然后您可以使用await,这样
您还可以调用
if(shouldStop)
检查的方法reset() will awaken all waiting threads with a thrown exception
you can then use the await as so
you can also call a method for the
if(shouldStop)
check从听起来你可能想要一个 CountDownLatch< /a>.假设您知道线程/参与者的数量,您只需为那么多线程/参与者创建一个,然后当您的线程完成倒计时并等待锁存器时:
与
CyclicBarrier
相比,CountDownLatch
不可重复使用,只能使用一次。然而,它确实将等待和释放问题分开,因此您可以拥有另一个允许线程通过的线程。话虽如此,如果您确实需要
CyclicBarrier
,上述代码的细微变化应该可以工作:但是,如果任何线程被中断或
barrier.reset()
被调用,然后屏障被破坏并抛出异常。From the sounds of it you probably want a CountDownLatch. Assuming you know the number of threads/participants, you simply create one for that many and then when your thread is done count down and await the latch:
Compared to
CyclicBarrier
,CountDownLatch
is not reusable, you only use it once. It does however separate the waiting and the releasing concerns, so you can for instance have another thread that allows the threads through.All that said, if you do need a
CyclicBarrier
a slight variation on the above code should work:however, if any thread is interrupted or
barrier.reset()
is called then barrier is broken and the exception is thrown.