同步輔助類CountDownLatch的例子
就是使用一個計數器作為鎖。在專案中遇到這樣一種情形,要處理一批訂單,處理完成之後需要報告成功多少,失敗多少。為了採用多執行緒機制,即每個訂單啟動一個執行緒(用執行緒池管理),當所有訂單完成之後,還要通知主執行緒進行統計報告,就想到了CountDownLatch。
下面是一個簡單的例子。
import java.util.concurrent.CountDownLatch;
public class CountDownLatchTest {
static class SimpleThread extends Thread {
private CountDownLatch latch;
public SimpleThread(CountDownLatch latch){
this.latch = latch;
}
@Override
public void run() {
System.out.println(this + " RUNNING.");
latch.countDown();
}
}
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(5);
for(int i=0; i<5; i++) {
new SimpleThread(latch).start();
}
//等待所有子執行緒處理完成。
latch.await();
System.out.println("Over");
}
}
執行結果:
Thread[Thread-2,5,main] RUNNING.
Thread[Thread-0,5,main] RUNNING.
Thread[Thread-1,5,main] RUNNING.
Thread[Thread-4,5,main] RUNNING.
Thread[Thread-3,5,main] RUNNING.
Over
下面是一個簡單的例子。
import java.util.concurrent.CountDownLatch;
public class CountDownLatchTest {
static class SimpleThread extends Thread {
private CountDownLatch latch;
public SimpleThread(CountDownLatch latch){
this.latch = latch;
}
@Override
public void run() {
System.out.println(this + " RUNNING.");
latch.countDown();
}
}
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(5);
for(int i=0; i<5; i++) {
new SimpleThread(latch).start();
}
//等待所有子執行緒處理完成。
latch.await();
System.out.println("Over");
}
}
執行結果:
Thread[Thread-2,5,main] RUNNING.
Thread[Thread-0,5,main] RUNNING.
Thread[Thread-1,5,main] RUNNING.
Thread[Thread-4,5,main] RUNNING.
Thread[Thread-3,5,main] RUNNING.
Over
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10742815/viewspace-611904/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 同步工具類—— CountDownLatchCountDownLatch
- Java多執行緒同步工具類之CountDownLatchJava執行緒CountDownLatch
- CountDownLatch 多執行緒同步輔助類用法CountDownLatch執行緒
- .NET非同步方法呼叫的例子非同步
- Java併發工具類(閉鎖CountDownLatch)JavaCountDownLatch
- Java 併發工具類 CountDownLatch、CyclicBarrier、Semaphore、ExchangerJavaCountDownLatch
- 非同步學習小例子非同步
- 併發工具類(一)等待多執行緒的CountDownLatch執行緒CountDownLatch
- Android開發 - 儲存輔助類 SharedPreferences 解析Android
- 大白話說java併發工具類-CountDownLatch,CyclicBarrierJavaCountDownLatch
- CountDownLatchCountDownLatch
- muduo網路庫學習之MutexLock類、MutexLockGuard類、Condition類、CountDownLatch類封裝中的知識點MutexCountDownLatch封裝
- 多執行緒與併發----CycliBarrier、CountDownLatch 和 Exchanger同步執行緒CountDownLatch
- 通用輔助生成: 使用任意輔助模型加速解碼模型
- python類例子(開槍)Python
- Java併發包5--同步工具CountDownLatch、CyclicBarrier、Semaphore的實現原理解析JavaCountDownLatch
- “追求平滑!”——針對射擊遊戲中吸附類/阻尼類輔助瞄準的平滑性最佳化探索遊戲
- CyclicBarrier And CountDownLatchCountDownLatch
- C++類和異常例子C++
- Java併發包原始碼學習系列:同步元件CountDownLatch原始碼解析Java原始碼元件CountDownLatch
- 07 併發工具類CountDownLatch、CyclicBarrier、Semaphore使用及原始碼分析CountDownLatch原始碼
- 詳解CountDownLatchCountDownLatch
- 手寫CountDownLatchCountDownLatch
- 一個類資料型別的STL例子 (轉)資料型別
- java併發程式設計JUC第九篇:CountDownLatch執行緒同步Java程式設計CountDownLatch執行緒
- java多執行緒10:併發工具類CountDownLatch、CyclicBarrier和SemaphoreJava執行緒CountDownLatch
- 更快的輔助生成: 動態推測
- CountDownLatch和CyclicBarrier的簡單使用CountDownLatch
- CyclicBarrier、CountDownLatch與Semaphore的小記CountDownLatch
- python3筆記---抽象類例子Python筆記抽象
- CountDownLatch原始碼解析CountDownLatch原始碼
- java原始碼-CountDownLatchJava原始碼CountDownLatch
- JUC:05.CountDownLatchCountDownLatch
- CountDownLatch原始碼分析CountDownLatch原始碼
- java CountDownLatch 詳解JavaCountDownLatch
- 用最簡單的例子講解js中解決非同步的方法JS非同步
- 四邊形輔助線做法
- 五,搭建環境:輔助功能