java中CyclicBarrier簡單入門使用,javacyclicbarrier

來源:互聯網
上載者:User

java中CyclicBarrier簡單入門使用,javacyclicbarrier

一個同步輔助類,它允許一組線程互相等待,直到到達某個公用屏障點 (common barrier point)。在涉及一組固定大小的線程的程式中,這些線程必須不時地互相等待,此時 CyclicBarrier 很有用。因為該 barrier 在釋放等待線程後可以重用,所以稱它為迴圈 的 barrier。

CyclicBarrier 支援一個可選的 Runnable 命令,在一組線程中的最後一個線程到達之後(但在釋放所有線程之前),該命令只在每個屏障點運行一次。若在繼續所有參與線程之前更新共用狀態,此屏障操作 很有用。 在java JDK中是這樣解釋的,在我們日常開發中常常需要一組線程結束之後,再接著進行,我們就需要考慮使用這個類了。

 執行之後列印如下結果:
線程 pool-1-thread-1即將到達線程集合點1,已有  1 到達繼續等候線程 pool-1-thread-3即將到達線程集合點1,已有  2 到達繼續等候線程 pool-1-thread-2即將到達線程集合點1,已有  3 到達都到齊,走吧線程 pool-1-thread-2即將到達線程集合點2,已有  1 到達繼續等候線程 pool-1-thread-1即將到達線程集合點2,已有  2 到達繼續等候線程 pool-1-thread-3即將到達線程集合點2,已有  3 到達都到齊,走吧線程 pool-1-thread-3即將到達線程集合點3,已有  1 到達繼續等候線程 pool-1-thread-1即將到達線程集合點3,已有  2 到達繼續等候線程 pool-1-thread-2即將到達線程集合點3,已有  3 到達都到齊,走吧
<pre name="code" class="java">import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.CyclicBarrier;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class CyclicBarrierTest {/** * @param args */public static void main(String[] args) {         ExecutorService ThreadPool = Executors.newCachedThreadPool();         final CyclicBarrier cyclic = new CyclicBarrier(3);         for(int i=0;i<3;i++){         Runnable runnable = new Runnable() {@Overridepublic void run() {                    try {                    Thread.sleep((long) (Math.random()*3000));                    System.out.println("線程 " + Thread.currentThread().getName() + "即將到達線程集合點1,已有  " + (cyclic.getNumberWaiting()+1) + " 到達" + (cyclic.getNumberWaiting()==2 ? "都到齊,走吧": "繼續等候"));cyclic.await(); Thread.sleep((long) (Math.random()*3000));                    System.out.println("線程 " + Thread.currentThread().getName() + "即將到達線程集合點2,已有  " + (cyclic.getNumberWaiting()+1) + " 到達" + (cyclic.getNumberWaiting()==2 ? "都到齊,走吧": "繼續等候"));cyclic.await(); Thread.sleep((long) (Math.random()*3000));                    System.out.println("線程 " + Thread.currentThread().getName() + "即將到達線程集合點3,已有  " + (cyclic.getNumberWaiting()+1) + " 到達" + (cyclic.getNumberWaiting()==2 ? "都到齊,走吧": "繼續等候"));cyclic.await(); } catch (InterruptedException e) {e.printStackTrace();} catch (BrokenBarrierException e) {e.printStackTrace();}}};ThreadPool.execute(runnable);         }         }}




,java中同步器的使用方法、

同步器Semaphore(一)
同步器CyclicBarrier(二)
同步器CountDownLatch(三)
同步器Exchanger(四)

想瞭解詳細:zheng12tian.iteye.com/category/149927
 
幫忙java程式解讀,人

import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; //引入程式所需要的包
class Player implements Runnable{ //實現runnable介面,實一個線程
private String name; //私人屬性name,下面有相應的get set方法
private CyclicBarrier barrier; // 麼有屬性barrier,一個同步輔助類,它允許一組線程互相等待,
//直到到達某個公用屏障點。在涉及一組固定大小的線程的程式中,
//這些線程必須不時地互相等待,此時 CyclicBarrier 很有用。因為該 barrier 在釋放等待線程後可以重用
private Player next; //執行個體本身
private int time; //整型time
private boolean run; //判斷是否正在運行
public Player(String name, CyclicBarrier barrier, boolean run) { //建構函式
super();
this.name = name;
this.setBarrier(barrier);
this.run = run;
}
public void run() { //實現runnable介面,必須實現的方法run
try {
synchronized (this) { //鎖住線程
while(!run){ //如果正在運行,就等待
wait();
}
}
Random r = new Random(); //數學輔助類
TimeUnit.MILLISECONDS.sleep(r.nextInt(6000)); 6000之內的整型隨機數,然後睡眠
next(next,11 + r.nextInt(2)); 調用next()方法
} catch (InterruptedException e) { //出現異常捕獲並列印
e.printStackTrace();
}
}
private v......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.