java CyclicBarrier
使用情況:你希望建立一組任務,他們並行執行,然後再進行下一個步驟之前等待,直到所有任務都完成
下面是一個賽馬的例子,挺有意思的(java程式設計思想)
import java.util.concurrent.*; import java.util.*; public class Horse implements Runnable { private static int counter=0; private final int id=counter++; private int strides=0; private static Random rand=new Random(47); private static CyclicBarrier barrier; public Horse(CyclicBarrier barrier){ this.barrier=barrier; } public synchronized int getStrides(){ return strides; } @Override public void run() { // TODO Auto-generated method stub try{ while(!Thread.interrupted()){ synchronized(this){ strides+=rand.nextInt(3); } barrier.await(); } } catch(InterruptedException e){} catch(BrokenBarrierException e){ throw new RuntimeException(e); } } public String toString(){ return "Horse "+id+" "; } public String tracks(){ StringBuilder s=new StringBuilder(); for(int i=0;i<getStrides();i++){ s.append("*"); } s.append(id); return s.toString(); } }
import java.util.concurrent.*;
import java.util.*;
public class HorseRace {
public static final int FINISH_LINE=75;
private List<Horse> horses =new ArrayList<Horse>();
private ExecutorService exec=Executors.newCachedThreadPool();
private CyclicBarrier barrier;
public HorseRace(int nHorses,final int pause){
barrier=new CyclicBarrier(nHorses,new Runnable(){
public void run(){
StringBuilder s=new StringBuilder();
for(int i=0;i<FINISH_LINE;i++)
s.append("=");
System.out.println(s);
for(Horse horse:horses)
System.out.println(horse.tracks());
for(Horse horse:horses){
if(horse.getStrides()>=FINISH_LINE){
System.out.println(horse+" won!");
exec.shutdownNow();
return;
}
try{
TimeUnit.MILLISECONDS.sleep(pause);
}catch(InterruptedException e){
System.out.println("barrier-action sleep interrupted");
}
}
}
});
for(int i=0;i<nHorses;i++){
Horse horse=new Horse(barrier);
horses.add(horse);
exec.execute(horse);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int nHorses=7;
int pause=200;
new HorseRace(nHorses,pause);
}
}
相關文章
- Java併發—— CountDownLatch與CyclicBarrierJavaCountDownLatch
- Java的迴圈屏障-CyclicBarrier(譯)Java
- Java 併發包之CountDownLatch、CyclicBarrierJavaCountDownLatch
- Java 多執行緒基礎 - CyclicBarrierJava執行緒
- Java高併發之CyclicBarrier簡介Java
- Java併發工具類(柵欄CyclicBarrier)Java
- Java併發之CountDownLatch、CyclicBarrier和SemaphoreJavaCountDownLatch
- Java 併發工具類 CountDownLatch、CyclicBarrier、Semaphore、ExchangerJavaCountDownLatch
- Java併發之CyclicBarrier(在集合點同步)Java
- CyclicBarrier 分析
- CyclicBarrier And CountDownLatchCountDownLatch
- Java多執行緒同步工具類之CyclicBarrierJava執行緒
- 大白話說java併發工具類-CountDownLatch,CyclicBarrierJavaCountDownLatch
- Java併發程式設計:CountDownLatch、CyclicBarrier和SemaphoreJava程式設計CountDownLatch
- Java 併發程式設計(十四) -- CyclicBarrier原始碼分析Java程式設計原始碼
- Java併發程式設計之CyclicBarrier使用指南Java程式設計
- CyclicBarrier原始碼解析原始碼
- CyclicBarrier原始碼分析原始碼
- java多執行緒10:併發工具類CountDownLatch、CyclicBarrier和SemaphoreJava執行緒CountDownLatch
- java多執行緒系列:通過對戰遊戲學習CyclicBarrierJava執行緒遊戲
- Java:多執行緒等待所有執行緒結束(CountDownLatch/CyclicBarrier) .Java執行緒CountDownLatch
- Java併發包原始碼學習系列:同步元件CyclicBarrier原始碼解析Java原始碼元件
- JUC(3)---CountDownLatch、CyclicBarrier和AQSCountDownLatchAQS
- CyclicBarrier – 同步屏障實現分析
- CyclicBarrier - 同步屏障實現分析
- java-多執行緒-CountDownLatch(閉鎖) CyclicBarrier(柵欄) Semaphore(訊號量)-Java執行緒CountDownLatch
- CountDownLatch和CyclicBarrier的簡單使用CountDownLatch
- CyclicBarrier、CountDownLatch與Semaphore的小記CountDownLatch
- Java併發包5--同步工具CountDownLatch、CyclicBarrier、Semaphore的實現原理解析JavaCountDownLatch
- java併發程式設計JUC第十篇:CyclicBarrier執行緒同步Java程式設計執行緒
- CountDownLatch和CyclicBarrier區別及詳解CountDownLatch
- 執行緒屏障CyclicBarrier實現原理執行緒
- 高併發之ReentrantLock、CountDownLatch、CyclicBarrierReentrantLockCountDownLatch
- 併發工具類(二)同步屏障CyclicBarrier
- CountDownLatch、CyclicBarrier、Semaphore、Exchanger 的詳細解析CountDownLatch
- CyclicBarrier、CountDownLatch以及Semaphore使用及其原理分析CountDownLatch
- 原始碼分析:CyclicBarrier 之迴圈柵欄原始碼
- 面試官:說說CountDownLatch,CyclicBarrier,Semaphore的原理?面試CountDownLatch