Java - 优雅地退出线程

发布于 2024-11-17 20:45:30 字数 644 浏览 3 评论 0原文

我有一堆执行计算的线程。它们使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一片旧的回忆 2024-11-24 20:45:30

reset() 将唤醒所有等待线程并抛出异常,

然后您可以使用await,这样

private static volatile boolean shouldStop=false;

public void run() {
    try{
        while (true) {
            if (someCondition) {
                // quit other threads when they call await()
                return;
            }
            try{
                if(shouldStop)return;
                barrier.await();
            }catch(BrokenBarrierException e){
                //someone stopped 
                return;
            }
       }
   }finally{
       shouldStop =true;
       barrier.reset();
   }
}

您还可以调用 if(shouldStop) 检查的方法

reset() will awaken all waiting threads with a thrown exception

you can then use the await as so

private static volatile boolean shouldStop=false;

public void run() {
    try{
        while (true) {
            if (someCondition) {
                // quit other threads when they call await()
                return;
            }
            try{
                if(shouldStop)return;
                barrier.await();
            }catch(BrokenBarrierException e){
                //someone stopped 
                return;
            }
       }
   }finally{
       shouldStop =true;
       barrier.reset();
   }
}

you can also call a method for the if(shouldStop) check

安静 2024-11-24 20:45:30

从听起来你可能想要一个 CountDownLatch< /a>.假设您知道线程/参与者的数量,您只需为那么多线程/参与者创建一个,然后当您的线程完成倒计时并等待锁存器时:

final int workers = …
final CountDownLatch latch = new CountDownLatch(workers);

void doSomething() throws InterruptedException {
  …
  latch.countDown();
  latch.await(); // blocks, throws InterruptedException
}

CyclicBarrier 相比,CountDownLatch不可重复使用,只能使用一次。然而,它确实将等待和释放问题分开,因此您可以拥有另一个允许线程通过的线程。

话虽如此,如果您确实需要 CyclicBarrier,上述代码的细微变化应该可以工作:

final int workers = …
final CyclicBarrier barrier = new CyclicBarrier(workers);

void doSomething() throws InterruptedException, BrokenBarrierException {
  …
  latch.await(); // blocks, throws InterruptedException, BrokenBarrierException
}

但是,如果任何线程被中断或 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:

final int workers = …
final CountDownLatch latch = new CountDownLatch(workers);

void doSomething() throws InterruptedException {
  …
  latch.countDown();
  latch.await(); // blocks, throws InterruptedException
}

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:

final int workers = …
final CyclicBarrier barrier = new CyclicBarrier(workers);

void doSomething() throws InterruptedException, BrokenBarrierException {
  …
  latch.await(); // blocks, throws InterruptedException, BrokenBarrierException
}

however, if any thread is interrupted or barrier.reset() is called then barrier is broken and the exception is thrown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文