1. 程式人生 > >CyclicBarrier核心程式碼dowait()方法分析(底層實現機制)

CyclicBarrier核心程式碼dowait()方法分析(底層實現機制)

引言:     相信大家對CyclicBarrier並不陌生,當一個執行緒呼叫了CyclicBarrier的await方法時,執行緒阻塞,直到指定數量的執行緒都執行完await方法後才喚醒所有執行緒。那麼CyclicBarrier的程式碼底層是如何實現的呢?它和CountDownLatch有什麼區別呢?
    很顯然,CyclicBarrier底層是使用ReentrantLock中的getCondition得到Condition,然後呼叫await和signalAll方法控制執行緒的阻塞和喚醒。
一、CyclicBarrier用法 12345678910111213141516二、await方法原始碼: 1
/**
2
     * Waits until all {@linkplain #getParties parties} have invoked
3
     * {@code await} on this barrier.
4
     *
5
     * <p>If the current thread is not the last to arrive then it is
6
     * disabled for thread scheduling purposes and lies dormant until
7
     * one of the following things happens:
8
     * <ul>
9
     * <li>The last thread arrives; or
10
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
11
     * the current thread; or
12
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
13
     * one of the other waiting threads; or
14
     * <li>Some other thread times out while waiting for barrier; or
15
     * <li>Some other thread invokes {@link #reset} on this barrier.
16
     * </ul>
17
     *
18
     * <p>If the current thread:
19
     * <ul>
20
     * <li>has its interrupted status set on entry to this method; or
21
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
22
     * </ul>
23
     * then {@link InterruptedException} is thrown and the current thread's
24
     * interrupted status is cleared.
25
     *
26
     * <p>If the barrier is {@link #reset} while any thread is waiting,
27
     * or if the barrier {@linkplain #isBroken is broken} when
28
     * {@code await} is invoked, or while any thread is waiting, then
29
     * {@link BrokenBarrierException} is thrown.
30
     *
31
     * <p>If any thread is {@linkplain Thread#interrupt interrupted} while waiting,
32
     * then all other waiting threads will throw
33
     * {@link BrokenBarrierException} and the barrier is placed in the broken
34
     * state.
35
     *
36
     * <p>If the current thread is the last thread to arrive, and a
37
     * non-null barrier action was supplied in the constructor, then the
38
     * current thread runs the action before allowing the other threads to
39
     * continue.
40
     * If an exception occurs during the barrier action then that exception
41
     * will be propagated in the current thread and the barrier is placed in
42
     * the broken state.
43
     *
44
     * @return the arrival index of the current thread, where index
45
     *         {@code getParties() - 1} indicates the first
46
     *         to arrive and zero indicates the last to arrive
47
     * @throws InterruptedException if the current thread was interrupted
48
     *         while waiting
49
     * @throws BrokenBarrierException if <em>another</em> thread was
50
     *         interrupted or timed out while the current thread was
51
     *         waiting, or the barrier was reset, or the barrier was
52
     *         broken when {@code await} was called, or the barrier
53
     *         action (if present) failed due to an exception
54
     */
55
    public int await() throws InterruptedException, BrokenBarrierException {
56
        try {
57
            return dowait(false, 0L);
58
        } catch (TimeoutException toe) {
59
            throw new Error(toe); // cannot happen
60
        }
61
    }
三、dowait方法的原始碼:

相關推薦

CyclicBarrier核心程式碼dowait()方法分析底層實現機制

引言:     相信大家對CyclicBarrier並不陌生,當一個執行緒呼叫了CyclicBarrier的await方法時,執行緒阻塞,直到指定數量的執行緒都執行完await方法後才喚醒

Linux核心中斷引入使用者空間非同步通知機制

當linux核心空間發生中斷後怎麼使使用者空間的應用程式執行相應的函式呢,當晶片有資料到來時核心會產生一箇中斷,但是怎樣通知應用程式來取資料,以前這個問題一直困擾我很長時間,後來發現linux中有非同步通知機制,在使用者程式中用signal註冊一個響應SIGIO訊號的回撥函式,然後在驅動程式中向該程

記錄一次失敗的向git提交程式碼,和解決的方法首次建立倉庫

背景: 向git push程式碼(建立一個新的倉庫) 做法: 在github建立一個新的倉庫------>本地新建資料夾------->依次執行了下面的命令 git init git clone git checkout -b master

EventBus原始碼分析:post方法釋出事件【獲取事件的所有訂閱者,反射呼叫訂閱者事件處理方法2.4版本

EventBus維護了一個重要的HashMap,這個HashMap的鍵是事件,值是該事件的訂閱者列表,因此post事件的時候就能夠從此HashMap中取出事件的訂閱者列表,對每個訂閱者反射呼叫事件處理方法。 private final Map<Cla

Elasticsearch 核心外掛Kibana 本地檔案包含漏洞分析CVE-2018-17246

不久前Elasticsearch釋出了最新安全公告, Elasticsearch Kibana 6.4.3之前版本和5.6.13之前版本中的Console外掛存在嚴重的本地檔案包含漏洞可導致拒絕服務攻擊、任意檔案讀取攻擊、配合第三方應用反彈SHELL攻擊,下文筆者對其漏洞背景、攻擊原理和行為進行分析和復現。

opencv人臉檢測程式碼應用與分析PC端和Android端

(1)OpenCV人臉檢測C++程式流程: OpenCV的人臉檢測程式採用了Viola & Jones人臉檢測方法,主要是呼叫訓練好的瀑布級聯分類器cascade來進行模式匹配。 cvHaarDetectObjects先將影象灰度化,根據傳入引數判斷是否進行can

hashcode衝突的解決方法以及原理分析收穫頗大

在Java程式語言中,最基本的結構就是兩種,一種是陣列,一種是模擬指標(引用),所有的資料結構都可以用這兩個基本結構構造,HashMap也一樣。當程式試圖將多個 key-value 放入 HashMap 中時,以如下程式碼片段為例: HashMap<String,Object> m=new

linux核心SPI匯流排驅動分析

下面有兩個大的模組: 一個是SPI匯流排驅動的分析            (研究了具體實現的過程) 另一個是SPI匯流排驅動的編寫(不用研究具體的實現過程) SPI匯流排驅動分析 1 SPI概述      SPI是英語Serial Peripheral inte

移動端 響應式、自適應、適配 實現方法分析和其他基礎知識拓展

.html ali pen isp 無法 ace eight window 會有 基礎拓展(最後補充是移動端適配可能兼容問題): 像素: 設備獨立(邏輯)像素:window.screen.width iphone6==>375手機屏幕大小:別名叫點(一個點裏面可以

轉:【Java並發編程】之十六:深入Java內存模型——happen-before規則及其對DCL的分析含代碼

無需 bit 對象引用 說了 final 緩存 機器 通過 round 轉載請註明出處:http://blog.csdn.net/ns_code/article/details/17348313 happen—before規則介紹 Java語言中有一個“先行發生

java中方法總結每周更新

實例 參數 創建 方法 get ack bject 子類 generate 1、URLEncoder.encode(username,"utf-8")將“utf-8”編碼的username先解碼,然後再采用URL編碼 2、URLDecoder.decode(autoLogi

FLV文件格式分析附源碼

src 16bit 腳本 ted end 介紹 ada string類型 orm FLV文件主要由兩部分組成:Header和Body。 1. Header header部分記錄了flv的類型、版本等信息,是flv的開頭,一般都差不多,占9bytes。具體格式如下: 文

單例模式及常見寫法分析設計模式01

啟動 nes 成員變量 額外 log 序列 spa tar adl 保證一個類僅有一個實例。並提供一個該實例的全局訪問點。 ——《設計模式》單例模式的概念非常easy。以下以C#語言為樣例,列出常見單例寫法的優缺點。1、簡單實現 public s

定義抽象類Shape,抽象方法為showArea(),求出面積並顯示,定義矩形類Rectangle,正方形類Square,圓類 Circle,根據各自的屬性,用showArea方法求出各自的面積,在main方法中構造3個對象,調用showArea方法體現多態

子類 protected new 都是 package 使用 類指針 3.1 shape 實現多態的三個條件:1.要有繼承2.要有抽象方法重寫3.用父類指針(引用)指向子類對象 重載重寫重定義的區別: 1.重載:在同一個類中進行;  編譯時根據參數類型和個數決定方法調用; 

深入淺出Mybatis系列---SQL執行流程分析源碼篇(轉)

factor demo 讀取配置 gist wrapper load 任性 wrap 深入淺出 轉載自:http://www.cnblogs.com/dongying/p/4142476.html 1. SqlSessionFactory 與 SqlSession.   通

Wireshark安裝使用及報文分析圖文詳解

p s 技術 cap cut .net 信息 display 過程 數據 Wireshark是世界上最流行的網絡分析工具。這個強大的工具可以捕捉網絡中的數據,並為用戶提供關於網絡和上層協議的各種信息。與很多其他網絡工具一樣,Wireshark也使用pcapnetwork l

Array對象的方法總結ES5 與 ES6

判斷 否則 array 復制 indexof define 字符 red 三個參數 ES5 數組方法 1.Array.isArray() 方法用來判斷一個值是否為數組。它可以彌補typeof運算符的不足 2.valueOf() 方法返回數組本身 3.toString()

C#設計模式之十三模板方法模式Template Method Pattern【行為型】

並集 client 變化 args 集中 pac 爸爸 rim 自己 原文:C#設計模式之十三模板方法模式(Template Method Pattern)【行為型】一、引言 “結構型”的設計模式已經寫完了,從今天我們開始講“行為型”設計模式。現在我們開始講【行為型】設

apollo2.0 lidar代碼分析未寫完

自動駕駛 激光雷達 1. 概述 // lidar輸出數據定義class Velodyne的產品RawFrame : public SensorRawFrame {public:VelodyneRawFrame() {}~VelodyneRawFrame() {}public:pclutil::Poin

python文件和目錄操作方法大全含實例

file python tree odin inux 長度 需要 tell mtr 一、python中對文件、文件夾操作時經常用到的os模塊和shutil模塊常用方法。 1 得到當前工作目錄,即當前Python腳本工作的目錄路徑: os.getcwd() 2 返回指定