欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解java CountDownLatch和CyclicBarrier在內部實現(xiàn)和場景上的區(qū)別

 更新時間:2020年05月20日 14:41:12   作者:wavebeed  
這篇文章主要介紹了詳解java CountDownLatch和CyclicBarrier在內部實現(xiàn)和場景上的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

CountDownLatch和CyclicBarrier兩個同為java并發(fā)編程的重要工具類,它們在諸多多線程并發(fā)或并行場景中得到了廣泛的應用。但兩者就其內部實現(xiàn)和使用場景而言是各有所側重的。

內部實現(xiàn)差異

前者更多依賴經(jīng)典的AQS機制和CAS機制來控制器內部狀態(tài)的更迭和計數(shù)器本身的變化,而后者更多依靠可重入Lock等機制來控制其內部并發(fā)安全性和一致性。

 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 {
  /**
   * Each use of the barrier is represented as a generation instance.
   * The generation changes whenever the barrier is tripped, or
   * is reset. There can be many generations associated with threads
   * using the barrier - due to the non-deterministic way the lock
   * may be allocated to waiting threads - but only one of these
   * can be active at a time (the one to which {@code count} applies)
   * and all the rest are either broken or tripped.
   * There need not be an active generation if there has been a break
   * but no subsequent reset.
   */
  private static class Generation {
    boolean broken = false;
  }

  /** The lock for guarding barrier entry */
  private final ReentrantLock lock = new ReentrantLock();
  /** Condition to wait on until tripped */
  private final Condition trip = lock.newCondition();
  /** The number of parties */
  private final int parties;
  /* The command to run when tripped */
  private final Runnable barrierCommand;
  /** The current generation */
  private Generation generation = new Generation();

  /**
   * Number of parties still waiting. Counts down from parties to 0
   * on each generation. It is reset to parties on each new
   * generation or when broken.
   */
  private int count;

  /**
   * Updates state on barrier trip and wakes up everyone.
   * Called only while holding lock.
   */
  private void nextGeneration() {
    // signal completion of last generation
    trip.signalAll();
    // set up next generation
    count = parties;
    generation = new Generation();
  }

  /**
   * Sets current barrier generation as broken and wakes up everyone.
   * Called only while holding lock.
   */
  private void breakBarrier() {
    generation.broken = true;
    count = parties;
    trip.signalAll();
  }

  /**
   * Main barrier code, covering the various policies.
   */
  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();
    }
  }
  ... ... //
 }

實戰(zhàn) - 展示各自的使用場景

/**
 *類說明:共5個初始化子線程,6個閉鎖扣除點,扣除完畢后,主線程和業(yè)務線程才能繼續(xù)執(zhí)行
 */
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");
      }
    }
  }

  /*業(yè)務線程等待latch的計數(shù)器為0完成*/
  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........");
  }
}
/**
 *類說明:共4個子線程,他們全部完成工作后,交出自己結果,
 *再被統(tǒng)一釋放去做自己的事情,而交出的結果被另外的線程拿來拼接字符串
 */
class UseCyclicBarrier {
  private static CyclicBarrier barrier
      = new CyclicBarrier(4,new CollectThread());

  //存放子線程工作結果的容器
  private static ConcurrentHashMap<String,Long> resultMap
      = new ConcurrentHashMap<String,Long>();

  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<String,Long> 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線程可以重復被執(zhí)行,通過多次觸發(fā)await()方法,countdownlatch可以調用await()方法多次;cyclicbarrier若沒有結果匯總,則調用一次await()就夠了;

2. New cyclicbarrier(threadCount)的線程數(shù)必須與實際的用戶線程數(shù)一致;

3. 協(xié)調線程同時運行:countDownLatch協(xié)調工作線程執(zhí)行,是由外面線程協(xié)調;cyclicbarrier是由工作線程之間相互協(xié)調運行;

4. 從構造函數(shù)上看出:countDownlatch控制運行的計數(shù)器數(shù)量和線程數(shù)沒有關系;cyclicbarrier構造中傳入的線程數(shù)等于實際執(zhí)行線程數(shù);

5. countDownLatch在不能基于執(zhí)行子線程的運行結果做處理,而cyclicbarrier可以;

6. 就使用場景而言,countdownlatch 更適用于框架加載前的一系列初始化工作等場景; cyclicbarrier更適用于需要多個用戶線程執(zhí)行后,將運行結果匯總再計算等典型場景;

到此這篇關于詳解java CountDownLatch和CyclicBarrier在內部實現(xiàn)和場景上的區(qū)別的文章就介紹到這了,更多相關java CountDownLatch和CyclicBarrier區(qū)別內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • spring-boot-maven-plugin引入出現(xiàn)爆紅(已解決)

    spring-boot-maven-plugin引入出現(xiàn)爆紅(已解決)

    這篇文章主要介紹了spring-boot-maven-plugin引入出現(xiàn)爆紅(已解決),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • JDK 14的新特性:文本塊Text Blocks的使用

    JDK 14的新特性:文本塊Text Blocks的使用

    這篇文章主要介紹了JDK 14的新特性:文本塊Text Blocks的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • Java調用SSE流式接口并流式返回給前端實現(xiàn)打字輸出效果

    Java調用SSE流式接口并流式返回給前端實現(xiàn)打字輸出效果

    在Web開發(fā)中,有時我們需要將文件以流的形式返回給前端,下面這篇文章主要給大家介紹了關于Java調用SSE流式接口并流式返回給前端實現(xiàn)打字輸出效果的相關資料,需要的朋友可以參考下
    2024-08-08
  • Java中StringBuilder字符串類型的操作方法及API整理

    Java中StringBuilder字符串類型的操作方法及API整理

    Java中的StringBuffer類繼承于AbstractStringBuilder,用來創(chuàng)建非線程安全的字符串類型對象,下面即是對Java中StringBuilder字符串類型的操作方法及API整理
    2016-05-05
  • SpringBoot集成thymeleaf渲染html模板的步驟詳解

    SpringBoot集成thymeleaf渲染html模板的步驟詳解

    這篇文章主要給大家詳細介紹了SpringBoot集成thymeleaf如何使實現(xiàn)html模板的渲染,文中有詳細的代碼示例,具有一定的參考價值,需要的朋友可以參考下
    2023-06-06
  • java返回集合為null還是空集合及空集合的三種寫法小結

    java返回集合為null還是空集合及空集合的三種寫法小結

    這篇文章主要介紹了java返回集合為null還是空集合及空集合的三種寫法小結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 淺談Spring Security LDAP簡介

    淺談Spring Security LDAP簡介

    這篇文章主要介紹了淺談Spring Security LDAP簡介,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • java實現(xiàn)單鏈表中的增刪改

    java實現(xiàn)單鏈表中的增刪改

    這篇文章主要為大家詳細介紹了java實現(xiàn)單鏈表中的增刪改,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Mybatis 級聯(lián)刪除的實現(xiàn)

    Mybatis 級聯(lián)刪除的實現(xiàn)

    這篇文章主要介紹了Mybatis 級聯(lián)刪除的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Java并發(fā)編程之線程池實現(xiàn)原理詳解

    Java并發(fā)編程之線程池實現(xiàn)原理詳解

    池化思想是一種空間換時間的思想,期望使用預先創(chuàng)建好的對象來減少頻繁創(chuàng)建對象的性能開銷,java中有多種池化思想的應用,例如:數(shù)據(jù)庫連接池、線程池等,下面就來具體講講
    2023-05-05

最新評論