phaser 使用

摘抄:
https://www.jianshu.com/p/1517a379fb91
可參考:
https://www.iteye.com/blog/shift-alt-ctrl-2302923

常用方法
1,設定任務數(這是官方解釋,簡單的講比如我們指定任務數爲3,那3個線程都運行到指定地方他們纔會分別繼續往下執行)

\\批量設置任務數
public int bulkRegister(int parties)
\\動態增加1個任務數
public int register()
\\減少一個任務數
public int arriveAndDeregister()

2,到達等待

\\線程執行到此開始等待,滿足條件(任務數滿足)則繼續執行
public int arriveAndAwaitAdvance() 
\\ 線程執行到此不用等待繼續執行,但會讓任務數加1,然後會重置任務數。
public int arrive()
\\傳入的phase表示翻越幾個屏障,如果傳入的等於getPhase則等待
public int awaitAdvance(int phase)
\\可中斷的awaitAdvance
public int awaitAdvanceInterruptibly(int phase);
\\可規定時間指定欄數未變,則拋出異常
public int awaitAdvanceInterruptibly(int phase,
                                         long timeout, TimeUnit unit)

3,得到欄數以及任務數

\\當前執行到第幾個屏障
public final int getPhase()
\\得到註冊的任務數
public int getRegisteredParties()
\\已經執行的任務數
public int getArrivedParties()
\\還未執行的任務數
public int getUnarrivedParties()

4,通過屏障時調用

\\該方法返回true則直接通過屏障 false則屏障繼續工作,要想實現自己的邏輯代碼需要複寫此方法
protected boolean onAdvance(int phase, int registeredParties)

5,關閉屏障

\\取消屏障
public void forceTermination()
\\phaser是否銷燬
public boolean isTerminated()
public class PharseDemo {

    public static void main(String[] args) throws InterruptedException {
        //10名運動員,每名運動員在同一時間點的開始
        Phaser pharse = new Phaser(10) {
            @Override
            protected boolean onAdvance(int phase, int registeredParties) {
                if(phase == 0) {
                    System.out.println("所有人員已經到賽場");
                }else if(phase == 1) {
                    System.out.println("所有人員準備完畢,開始比賽");
                }else if(phase == 2) {
                    System.out.println("比賽結束");
                }
                return super.onAdvance(phase, registeredParties);
            }
        };
        //創建10個線程
        Runner run = new Runner(pharse);
        Thread[] thread  = new Thread[10];
        for (int i = 0; i < 10; i++) {
            thread[i] = new Thread(run);
        }
        Thread.sleep(3000);
        for (int i = 0; i < 10; i++) {
            thread[i].start();
        }
    }
    
}

class Runner implements Runnable{

    private Phaser phaser;
    Random random = new Random();
    
    public Runner(Phaser phaser) {
        super();
        this.phaser = phaser;
    }



    @Override
    public void run() {
        try {
            //第一階段  到達賽場 每個運動員情況不一樣
            Thread.sleep(random.nextInt(3000));
            System.out.println(Thread.currentThread().getName()+"已經到達賽場");
            phaser.arriveAndAwaitAdvance();
            //第二階段   開始準備 
            Thread.sleep(random.nextInt(3000));
            System.out.println(Thread.currentThread().getName()+"已經準備好");
            phaser.arriveAndAwaitAdvance();
            //第二階段   到達終點
            Thread.sleep(random.nextInt(3000));
            System.out.println(Thread.currentThread().getName()+"已經到達終點");
            phaser.arriveAndAwaitAdvance();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章