栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

详解java CountDownLatch和CyclicBarrier在内部实现和场景上的区别

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解java CountDownLatch和CyclicBarrier在内部实现和场景上的区别

前言

CountDownLatch和CyclicBarrier两个同为java并发编程的重要工具类,它们在诸多多线程并发或并行场景中得到了广泛的应用。但两者就其内部实现和使用场景而言是各有所侧重的。

内部实现差异

前者更多依赖经典的AQS机制和CAS机制来控制器内部状态的更迭和计数器本身的变化,而后者更多依靠可重入Lock等机制来控制其内部并发安全性和一致性。

 public class {
   //Synchronization control For CountDownLatch.
   //Uses AQS state to represent count.
  private static final class Sync extends AbstractQueuedSynchronizer {
    private static final long serialVersionUID = 4982264981922014374L;

    Sync(int count) {
      setState(count);
    }

    int getCount() {
      return getState();
    }

    protected int tryAcquireShared(int acquires) {
      return (getState() == 0) ? 1 : -1;
    }

    protected boolean tryReleaseShared(int releases) {
      // Decrement count; signal when transition to zero
      for (;;) {
 int c = getState();
 if (c == 0)
   return false;
 int nextc = c-1;
 if (compareAndSetState(c, nextc))
   return nextc == 0;
      }
    }
  }

  private final Sync sync;
  ... ...//
 }
 public class CyclicBarrier {
  
  private static class Generation {
    boolean broken = false;
  }

  
  private final ReentrantLock lock = new ReentrantLock();
  
  private final Condition trip = lock.newCondition();
  
  private final int parties;
  
  private final Runnable barrierCommand;
  
  private Generation generation = new Generation();

  
  private int count;

  
  private void nextGeneration() {
    // signal completion of last generation
    trip.signalAll();
    // set up next generation
    count = parties;
    generation = new Generation();
  }

  
  private void breakBarrier() {
    generation.broken = true;
    count = parties;
    trip.signalAll();
  }

  
  private int dowait(boolean timed, long nanos)
    throws InterruptedException, BrokenBarrierException,
 TimeoutException {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
      final Generation g = generation;

      if (g.broken)
 throw new BrokenBarrierException();

      if (Thread.interrupted()) {
 breakBarrier();
 throw new InterruptedException();
      }

      int index = --count;
      if (index == 0) { // tripped
 boolean ranAction = false;
 try {
   final Runnable command = barrierCommand;
   if (command != null)
     command.run();
   ranAction = true;
   nextGeneration();
   return 0;
 } finally {
   if (!ranAction)
     breakBarrier();
 }
      }

      // loop until tripped, broken, interrupted, or timed out
      for (;;) {
 try {
   if (!timed)
     trip.await();
   else if (nanos > 0L)
     nanos = trip.awaitNanos(nanos);
 } catch (InterruptedException ie) {
   if (g == generation && ! g.broken) {
     breakBarrier();
     throw ie;
   } else {
     // We're about to finish waiting even if we had not
     // been interrupted, so this interrupt is deemed to
     // "belong" to subsequent execution.
     Thread.currentThread().interrupt();
   }
 }

 if (g.broken)
   throw new BrokenBarrierException();

 if (g != generation)
   return index;

 if (timed && nanos <= 0L) {
   breakBarrier();
   throw new TimeoutException();
 }
      }
    } finally {
      lock.unlock();
    }
  }
  ... ... //
 }

实战 - 展示各自的使用场景


public class UseCountDownLatch {
  
  static CountDownLatch latch = new CountDownLatch(6);

  
  private static class InitThread implements Runnable{

    public void run() {
      System.out.println("Thread_"+Thread.currentThread().getId()
  +" ready init work......");
      latch.countDown();
      for(int i =0;i<2;i++) {
 System.out.println("Thread_"+Thread.currentThread().getId()
    +" ........continue do its work");
      }
    }
  }

  
  private static class BusiThread implements Runnable{

    public void run() {
      try {
 latch.await();
      } catch (InterruptedException e) {
 e.printStackTrace();
      }
      for(int i =0;i<3;i++) {
 System.out.println("BusiThread_"+Thread.currentThread().getId()
    +" do business-----");
      }
    }
  }

  public static void main(String[] args) throws InterruptedException {
    new Thread(new Runnable() {
      public void run() {
 SleepTools.ms(1);
 System.out.println("Thread_"+Thread.currentThread().getId()
    +" ready init work step 1st......");
 latch.countDown();
 System.out.println("begin step 2nd.......");
 SleepTools.ms(1);
 System.out.println("Thread_"+Thread.currentThread().getId()
    +" ready init work step 2nd......");
 latch.countDown();
      }
    }).start();
    new Thread(new BusiThread()).start();
    for(int i=0;i<=3;i++){
      Thread thread = new Thread(new InitThread());
      thread.start();
    }
    latch.await();
    System.out.println("Main do ites work........");
  }
}

class UseCyclicBarrier {
  private static CyclicBarrier barrier
      = new CyclicBarrier(4,new CollectThread());

  //存放子线程工作结果的容器
  private static ConcurrentHashMap resultMap
      = new ConcurrentHashMap();

  public static void main(String[] args) {
    for(int i=0;i<4;i++){
      Thread thread = new Thread(new SubThread());
      thread.start();
    }

  }

  
  private static class CollectThread implements Runnable{

    @Override
    public void run() {
      StringBuilder result = new StringBuilder();
      for(Map.Entry workResult:resultMap.entrySet()){
 result.append("["+workResult.getValue()+"]");
      }
      System.out.println(" the result = "+ result);
      System.out.println("do other business........");
    }
  }

  
  private static class SubThread implements Runnable{
    @Override
    public void run() {
      long id = Thread.currentThread().getId();
      resultMap.put(Thread.currentThread().getId()+"",id);
      try {
   Thread.sleep(1000+id);
   System.out.println("Thread_"+id+" ....do something ");
 barrier.await();
 Thread.sleep(1000+id);
 System.out.println("Thread_"+id+" ....do its business ");
 barrier.await();
      } catch (Exception e) {
 e.printStackTrace();
      }
    }
  }
}


 两者总结

1. Cyclicbarrier结果汇总的Runable线程可以重复被执行,通过多次触发await()方法,countdownlatch可以调用await()方法多次;cyclicbarrier若没有结果汇总,则调用一次await()就够了;

2. New cyclicbarrier(threadCount)的线程数必须与实际的用户线程数一致;

3. 协调线程同时运行:countDownLatch协调工作线程执行,是由外面线程协调;cyclicbarrier是由工作线程之间相互协调运行;

4. 从构造函数上看出:countDownlatch控制运行的计数器数量和线程数没有关系;cyclicbarrier构造中传入的线程数等于实际执行线程数;

5. countDownLatch在不能基于执行子线程的运行结果做处理,而cyclicbarrier可以;

6. 就使用场景而言,countdownlatch 更适用于框架加载前的一系列初始化工作等场景; cyclicbarrier更适用于需要多个用户线程执行后,将运行结果汇总再计算等典型场景;

到此这篇关于详解java CountDownLatch和CyclicBarrier在内部实现和场景上的区别的文章就介绍到这了,更多相关java CountDownLatch和CyclicBarrier区别内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/134007.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号