【问题标题】:Application hangs when I invoke join inside CyclicBarrier callback当我在 CyclicBarrier 回调中调用 join 时应用程序挂起
【发布时间】:2015-11-02 00:32:50
【问题描述】:

我在开始线程的地方有以下方法测试:

public static void main(String[] args) throws InterruptedException {
        List<Thread> threads = new ArrayList<>();
        final CyclicBarrier cyclicBarrier = new CyclicBarrier(1);
        Thread thread = new Thread(new CallbackThread(cyclicBarrier, threads));
        threads.add(thread);
        thread.start();          
    }

CallBack 线程如下所示:

class CallbackThread implements Runnable {
    CyclicBarrier cyclicBarrier;
    List<Thread> threads;

    CallbackThread(CyclicBarrier cyclicBarrier, List<Thread> threads) {
        this.cyclicBarrier = cyclicBarrier;
        this.threads = threads;
    }

    @Override
    public void run() {
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (BrokenBarrierException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        System.out.println("Threads started");
        for (Thread thread1 : threads) {
            try {
                thread1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        System.out.println("Threads finished");

    }
}

当我运行应用程序时,我看到以下输出:

Threads started

应用程序挂起。

我不明白为什么。

如果将连接逻辑替换为 main 方法 - 一切顺利。

public static void main(String[] args) throws InterruptedException {
        List<Thread> threads = new ArrayList<>();
        final CyclicBarrier cyclicBarrier = new CyclicBarrier(1);
        Thread thread = new Thread(new CallbackThread(cyclicBarrier, threads));
        threads.add(thread);
        thread.start();
        for (Thread thread1 : threads) {
            try {
                thread1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        System.out.println("Threads finished");

    }

你能解释一下这个区别吗?
恕我直言,它应该可以工作。

【问题讨论】:

  • 您不应添加新问题,而应编辑此问题以给出正确的示例。我已投票结束您的新问题并将其标记为供版主注意。
  • @Narendra Pathai 实际上我应该删除这个问题,但我不能,因为它有答案
  • @Narendra Pathai 认真阅读。它不是重复的

标签: java multithreading concurrency cyclicbarrier


【解决方案1】:

第一个示例中的代码在其自己的线程上调用 join。您将它添加到列表中,线程会遍历列表并加入列表中的每个线程。

【解决方案2】:

你正在加入 self。这就是为什么节目永远不会结束的原因。

当您从主线程调用join() 时,主线程正在尝试加入它创建的线程CallableThread。所以它是正确的。

但是当您加入CallableThread 时,您将传递包含对自身的引用的threads[]。所以它正在加入自己,这不会结束。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-03
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
相关资源
最近更新 更多