java barrier_Java - Latch和Barrier的區別

之所以把Latch與Barrier放在一起比較是因為他們給人一種相似的感覺。

他們都是阻塞一些行為直至某個事件發生,但Latch是等待某個事件發生,而Barrier是等待線程。

先比較一下JCIP中對二者的描述:

Latch

A latch is a synchronizer that can delay the progress of threads until it reaches its terminal state.

A latch acts as a gate: until the latch reaches the terminal state the gate is closed and no thread can pass, and in the terminal state the gate opens, allowing all threads to pass.

Once the latch reaches the terminal state, it cannot change state again, so it remains open forever.

Latches can be used to ensure that certain activities do not proceed until other one-time activities complete。

即,閉鎖可以延遲線程執行直至達到相應的結束狀態。閉鎖就像一個大門,未到達結束狀態相當于大門緊閉,不讓任何線程通過。

而到達結束狀態后,大門敞開,讓所有的線程通過,但是一旦敞開后不會再關閉。

閉鎖可以用來確保一些活動在某個事件發生后執行。

Barrier

CyclicBarrier allows a fixed number of parties to rendezvous repeatedly at a barrier point and is useful in parallel iterative algorithms that break down a problem into a fixed number of independent subproblems.

Threads call await when they reach the barrier point, and await blocks until all the threads have reached the barrier point.

If all threads meet at the barrier point, the barrier has been successfully passed, in which case all threads are released and the barrier is reset so it can be used again.

很多人都把Barrier直譯為"柵欄",我也很喜歡這個叫法。

柵欄可以使一組執行在一處匯集,也就是說我們可以用柵欄將一個問題分解成多個獨立的子問題,并在執行結束后在同一處進行匯集。

當線程到達匯集地后調用await,await方法會出現阻塞直至其他線程也到達匯集地。

如果所有的線程都到達就可以通過柵欄,也就是所有的線程得到釋放,而且柵欄也可以被重新利用。

另外,下面javadoc中對二者之間區別的說明:

A CountDownLatch is initialized with a given count.

The await methods block until the current count reaches zero due to invocations of the countDown method, after which all waiting threads are released and any subsequent invocations of await return immediately.

This is a one-shot phenomenon -- the count cannot be reset. If you need a version that resets the count, consider using a CyclicBarrier.

閉鎖從來都是帶著事件的觸發次數。

await方法會一直阻塞至countDown方法將次數變成0為止,所有的線程被釋放才能進行后續的工作。

但這種現象只能出現一次,也就是說觸發次數不會被重置。

如果你想要一個可重置次數的閉鎖,那就用柵欄。

Another typical usage would be to divide a problem into N parts, describe each part with a Runnable that executes that portion and counts down on the latch, and queue all the Runnables to an Executor.

When all sub-parts are complete, the coordinating thread will be able to pass through await.

(When threads must repeatedly count down in this way, instead use a CyclicBarrier.)

這種行為阻塞的典型用法之一就是將某個問題分成多個部分,每個部分用不同的線程負責,并記得減少閉鎖設置的次數。

當所有線程的工作結束后將通過await方法造成的阻塞,如果我們需要反復進行這樣的工作就需要使用柵欄。

好了,既然Doug Lea老師將同一個觀點闡述了這么多遍,剩下就是放心大膽地使用了,也許我們將問題想得太復雜了。

下面貼出栗子,由一個startGate攔住所有線程的執行,當所有線程就緒完成后調用countDown將它們釋放,而另一扇大門——endGate后面正等著計算執行時間,而endGate等待的事件由這些線程觸發:

public class TestHarness {

public static long timeTasks(int nThreads, final Runnable task)

throws InterruptedException {

final CountDownLatch startGate = new CountDownLatch(1);

final CountDownLatch endGate = new CountDownLatch(nThreads);

for (int i = 0; i < nThreads; i++) {

Thread t = new Thread() {

public void run() {

try {

startGate.await();

try {

task.run();

} finally {

endGate.countDown();

}

} catch (InterruptedException ignored) {

}

}

};

t.start();

}

long start = System.nanoTime();

startGate.countDown();

endGate.await();

long end = System.nanoTime();

return end - start;

}

}

執行看看:

public static void main(String[] args) throws ExecutionException, InterruptedException {

System.out.println("cost :::"+TestHarness.timeTasks(10,new Runnable() {

@Override

public void run() {

int num = RandomUtils.nextInt(0,100);

if(num>50) try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("Alvez ::"+ num);

}

}));

}

接著試試柵欄,沒有搞任何復雜的東西,注意countSupposed%partyCount,主要是想用這個體現一下柵欄是否可以反復使用多次。

如果countSupposed%partyCount的結果恰好為0,所有線程執行結束后,主線程也會正常結束。

反之則會一直阻塞下去,如果countSupposed%partyCount結果大于1且不為0,其結果就是我們看到"let's go to the barrier !!"出現的次數:

public static void barrierTest(int partyCount, int countSupposed) {

if(partyCount<1 || countSupposed < 1) throw new IllegalArgumentException();

final CyclicBarrier barrier = new CyclicBarrier(partyCount,new Runnable() {

@Override

public void run() {

System.out.println("let's go barrier !!");

}

});

System.out.println(countSupposed%partyCount==0?"let's show the smooth!!":"....but blocked");

for (int i = 0; i < countSupposed; i++) {

Runnable runnable = new Runnable() {

@Override

public void run() {

try {

barrier.await();

System.out.println(Thread.currentThread().getName() + " show!!");

} catch (InterruptedException e) {

e.printStackTrace();

} catch (BrokenBarrierException e) {

e.printStackTrace();

}

}

};

new Thread(runnable).start();

}

}

執行:

public static void main(String[] args) {

barrierTest(11, 20);

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/538468.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/538468.shtml
英文地址,請注明出處:http://en.pswp.cn/news/538468.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Kingback小組沖刺博客

一、6月13日 1、今日完成的任務 李可欣 項目首頁的框架設計 項目首頁的導航欄部分 對于項目的配色進行了改進&#xff0c;并且設計了logo 楊帆 今天進行了數據庫的設計&#xff0c;完成了數據庫的增刪改查&#xff0c;并建立了兩個jsp頁面&#xff0c;還學習了一些數據庫設計的…

用python前端html后端django_淺談Django前端后端值傳遞問題

前端后端傳值問題總結前端傳給后端通過表單傳值1、通過表單get請求傳值在前端當通過get的方式傳值時&#xff0c;表單中的標簽的name值將會被當做action的地址的參數此時&#xff0c;在后端可以通過get請求相應的name值拿到對應的value值例子:html中:{% csrf_token %}類別{% fo…

使用Git上傳本地項目到oschina

原文地址&#xff1a;https://my.oschina.net/158/blog/530112 --------------------------------------------- 1.首先看一下自己是否有公鑰&#xff0c; 在 我的資料-->SSH公鑰 查看&#xff0c;如果沒有&#xff0c;添加自己的SSH 公鑰&#xff1a; SSH key 可以讓你在…

手動創建git忽略push清單,node_module以及自身

1.文件夾內右鍵git bash&#xff0c;輸 touch .gitignore&#xff0c;注意中間有空格 2.編輯器打開生成的 .gitignore 文件&#xff0c;加入 .gitignore node_modules12 3.保存 4.以后再push到倉庫的就會過濾這倆了&#xff0c;想加啥自己加

整數數組按絕對值排序

2019獨角獸企業重金招聘Python工程師標準>>> 題目&#xff1a; 給出一組整數數組&#xff0c;要求按照絕對值從小到大進行排序后輸出。 可以用經典的冒泡排序法來計算此問題&#xff0c;計算過程&#xff1a; 1、比較相鄰兩個元素絕對值&#xff0c;如果第一個比第二…

python定時任務是異步的嗎_定時任務、異步任務

------------定時任務、異步任務--------pip install celery --targetD:\Users\ex-ouyangl003\PycharmProjects\data_new\dg_meta_system\metadata_system\venv\Lib\site-packages# coding:utf-8from apscheduler.schedulers.blocking import BlockingSchedulerimport datetime…

java excel自動保存_java讀取excel的內容(可保存到數據庫中)

//**poi jar包//public classReadExcel {SuppressWarnings("static-access")private staticString getValue(HSSFCell hssfCell) {if (hssfCell.getCellType() hssfCell.CELL_TYPE_BOOLEAN) {//返回布爾類型的值returnString.valueOf(hssfCell.getBooleanCellValue()…

《快學Scala》勘誤

原文鏈接&#xff1a;http://www.blogjava.net/sean/archive/2012/11/15/391386.html ---------------- 第11頁&#xff08;練習&#xff09;&#xff1a; 在Scala REPL中鍵入3&#xff0c;然后按Tab鍵 應為 在Scala REPL中鍵入3.&#xff0c;然后按Tab鍵 第19頁&#xff0…

二分排序java實現

1.什么是二分排序&#xff1a; 二分排序是指利用二分法的思想對插入排序進行改進的一種插入排序算法&#xff0c;不同于二叉排序&#xff0c;可以利用數組的特點快速定位指定索引的元素&#xff1b; 算法思想&#xff1a;二分法插入排序是在插入第i個元素時&#xff0c;對前面的…

pearson相關系數_pearson相關系數與典型相關性分析(CCA)

本文主要介紹相關系數的概念&#xff0c;以及簡單相關系數中的pearson相關系數及其局限性。隨后介紹pearson相關系數無法解決的問題(兩個變量組之間的相關性問題)的解決方案。1、pearson相關系數在日常中&#xff0c;我們經常會遇到一些關于相關性的分析&#xff0c;例如&#…

快學Scala習題解答—第三章 數組相關操作

原文鏈接&#xff1a;http://blog.csdn.net/ivan_pig/article/details/8257365 -------------------------------------------------- 4 數組相關操作 4.1 編寫一段代碼&#xff0c;將a設置為一個n個隨機整數的數組&#xff0c;要求隨機數介于0(包含)和n(不包含)之間 random和…

seo自動工具_愛站SEO工具包詳細介紹

愛站SEO工具-seoer的瑞士軍刀&#xff01;這個工具主要是為了方便SEOer查詢一些網站的問題&#xff0c;監控關鍵詞排名收錄等等&#xff0c;新手老手都可以用的工具&#xff0c;更快的讓SEOer上手。相信有很多SEOer都使用過愛站SEO工具包&#xff0c;也有很多新入行的小伙伴可能…

人物三(依芙蒂法)

轉載于:https://www.cnblogs.com/song1900/p/9189921.html

常用Oracle分析函數詳解

原文鏈接&#xff1a;http://www.cnblogs.com/benio/archive/2011/06/01/2066106.html --------------------------------------------------------------------------- 學習步驟&#xff1a; 1. 擁有Oracle EBS demo 環境 或者 PROD 環境 2. copy以下代碼進 PL/SQL 3. 配合解…

XML文件結構和基本語法

XML文件的結構性內容&#xff0c;包括節點關系以及屬性內容等等。元素是組成XML的最基本的單位&#xff0c;它由開始標記&#xff0c;屬性和結束標記組成。就是一個元素的例子&#xff0c;每個元素必須有一個元素名&#xff0c;元素可以若干個屬性以及屬性值。 xml文件和html文…

python表格數據分類聚合_3-python數據分析-pandas高級操作之替換、映射、隨機抽樣、分組、高級數據聚合、數據加載、透視表、交叉表...

3-python數據分析-pandas高級操作之替換、映射、隨機抽樣、分組、高級數據聚合、數據加載、透視表、交叉表替換操作 replace替換操作可以同步作用于Series和DataFrame中單值替換普通替換&#xff1a; 替換所有符合要求的元素:to_replace15,value’e’按列指定單值替換&#xff…

oracle-SQL-case when 改用 DECODE

SELECT CASE FLOOR_LINE_ID WHEN 1 THEN 高鐵 WHEN 2 THEN 高速 WHEN 3 THEN 公路 WHEN 5 THEN 地鐵 ELSE 其他 END AS LINE_NAME, FLOOR_LINE_ID FROM ( SELECT FLOOR(LINE_ID/100) AS FLOOR_LINE_ID FROM DT4_LINE_NAME ) 改…

lcp mysql cluster_Mysql Cluster 非root用戶啟動ndbd節點報錯

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓1.配置文件&#xff0c;如下&#xff1a;[rootcent178 ~]# ls -lart /etc/my.cnf-rw-rw-r-- 1 mysql mysql 3055 Oct 31 17:29 /etc/my.cnf2.集群數據存儲文件夾&#xff0c;如下&#xff1a;[rootcent178 ~]# ls -lart /var/lib/m…

fatal: Could not read from remote repository.的解決辦法

原文地址&#xff1a;http://blog.csdn.net/huahua78/article/details/52330792 --------------------------------------------------------------------------------- 查看遠端地址 git remote –v 查看配置 git config --list git status git add . // 暫存所有的更改git…

python中mako中loop_python中Mako庫實例用法

Mako是一個模板庫。一種嵌入式的語言&#xff0c;能夠實現簡化組件布局以及繼承&#xff0c;主要的用途也是和作用域有關&#xff0c;但是效果是最直接切靈活的&#xff0c;這些都是mako的基本功能&#xff0c;掌握了基礎內容&#xff0c;接下來就是詳細的了解講述&#xff0c;…