首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与CyclicBarrier的死锁?

与CyclicBarrier的死锁?
EN

Stack Overflow用户
提问于 2017-07-21 17:16:37
回答 1查看 266关注 0票数 0

我对下面这段用于CyclicBarrier的代码有问题。

代码语言:javascript
复制
    MyJavaCyclicBarrierExample.java:::::::

            import java.util.Date; 
            import java.util.concurrent.BrokenBarrierException; 
            import java.util.concurrent.CyclicBarrier; 

            public class MyJavaCyclicBarrierExample { 

              public static void main(String[] args) { 

                  //3 threads are part of the barrier, ServiceOne, ServiceTwo and this main thread calling them. 

                  final CyclicBarrier barrier = new CyclicBarrier(2); 
                  Thread serviceOneThread = new Thread(new ServiceOne(barrier)); 
                  Thread serviceTwoThread = new Thread(new ServiceTwo(barrier)); 

                  System.out.println("Starting both the services at "+new Date()); 

                  serviceOneThread.start(); 
                  serviceTwoThread.start(); 

                  //Lets say main also has to do some work
                  try {
                      System.out.println("Main is going to do some work....");
                      Thread.sleep(10000);
                      System.out.println("Main has finished its work....");
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }

                  try {
                      System.out.println("Main is now going to wait at the barrier....");
                      barrier.await();
                      System.out.println("Main woken up at the barrier....");
                  } catch (InterruptedException e) { 
                      System.out.println("Main Thread interrupted!"); 
                      e.printStackTrace(); 
                  } catch (BrokenBarrierException e) { 
                      System.out.println("Main Thread interrupted!"); 
                      e.printStackTrace(); 
                  } 
                  System.out.println("Ending both the services at "+new Date()); 
              } 
            } 


        ServiceOne.java :::::::::
        import java.util.concurrent.BrokenBarrierException; 
        import java.util.concurrent.CyclicBarrier; 

        public class ServiceOne implements Runnable { 
            private final CyclicBarrier cyclicBarrier; 

            public ServiceOne(CyclicBarrier cyclicBarrier) { 
                this.cyclicBarrier = cyclicBarrier; 
            }

            @Override
            public void run() { 
                System.out.println("Starting service One..."); 
                try { 
                    Thread.sleep(3000);   //assuming service one does some business logic here...
                } catch (InterruptedException e1) { 
                    e1.printStackTrace(); 
                } 
                System.out.println("Service One has finished its work... waiting for others..."); 
                try {
                        System.out.println("ServiceOne is now going to wait at the barrier....");
                        cyclicBarrier.await();      //Let's wait for the other threads at the cyclic barrier.
                        System.out.println("ServiceOne woken up at the barrier....");
                } catch (InterruptedException e) { 
                   System.out.println("Service one interrupted!"); 
                   e.printStackTrace(); 
                } catch (BrokenBarrierException e) { 
                   System.out.println("Service one interrupted!"); 
                   e.printStackTrace(); 
                } 
                System.out.println("The wait is over, lets complete Service Two!"); 
            } 
        } 



ServiceTwo.java:::::::::

import java.util.concurrent.BrokenBarrierException; 
import java.util.concurrent.CyclicBarrier; 
public class ServiceTwo implements Runnable { 
    private final CyclicBarrier cyclicBarrier; 
    public ServiceTwo(CyclicBarrier cyclicBarrier) { 
        this.cyclicBarrier = cyclicBarrier; 
    } 
    @Override 
    public void run() { 
        System.out.println("Starting service Two...."); 
        try { 
            Thread.sleep(2000);             //assuming service one does some business logic here...
        } catch (InterruptedException e1) { 
            e1.printStackTrace(); 
        } 
        System.out.println("Service Two has finished its work.. waiting for others..."); 
        try { 
            System.out.println("ServiceTwo is now going to wait at the barrier....");
            cyclicBarrier.await();                  //Let's wait for the other threads at the cyclic barrier.
            System.out.println("ServiceTwo woken up at the barrier....");
        } catch (InterruptedException e) { 
            System.out.println("Service one interrupted!"); 
            e.printStackTrace(); 
        } catch (BrokenBarrierException e) { 
            System.out.println("Service one interrupted!"); 
            e.printStackTrace(); 
        } 
        System.out.println("The wait is over, lets complete Service One!"); 
    } 
} 

我的问题是,当我在两个级别上使用CyclicBarrier运行这段代码时,它似乎总是以死锁告终。然而,当我使用CyclicBarrier在级别1或3运行代码时,即新的CyclicBarrier(1)或新的CyclicBarrier(3),它总是成功完成。那么第二级的问题是什么呢?

EN

回答 1

Stack Overflow用户

发布于 2017-12-04 00:34:15

CyclicBarrier是循环的,这意味着它可以被重用。当屏障用argument 2初始化时,在被serviceOneThreadserviceTwoThread跳闸后,新的一代开始。主线程不能单独触发它。

也许你需要一个CountDownLatch

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45233800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档