理解java并发工具Phaser
java為我們提供了很多并發工具,比如Semaphore、CountDownLatch、CyclicBarrier還有我們這里要講到的phaser。
當某些任務是分成多個步驟來執行時,并且同一個步驟之間可以并發,不同步驟之間不能并發的時候, 此機制是非常有用的。Phaser類提供的機制是在每個步驟的結尾同步線程,所以除非全部線程完成第一個步驟,否則線程不能開始進行第二步。
使用phaser你首先需要指示參與同步的線程的個數,我們可以使用 phaser.register()或者new Phaser(5),下面的示意圖給出了形象的解釋:
我們假設每個方框代表需要同步的線程,這里直接使用new Phaser(4)說明有4個線程需要同步:
如果在同一個phaser對象上使用phaser.register(),每使用一次就增加一個需要同步的線程。也就是增加一個方框的數量。
如果現在有線程調用了:phaser.arriveAndAwaitAdvance()那就說明有一個線程到達了同步點,它將阻塞在調用這個函數的位置,直到所有需要同步的線程都到達,將對所有線程統一放行,進行第二個階段任務的執行。
在第二階段的時候,又會有這四個方框被創建出來,等待線程的到達,然后又統一放行,最后直到方框數量為0,方框數量是如何降到0的,就需要了解如果減少方框的數量了。
我們在執行期間,可以去增加或者減少這些方框的數量。增加的話依舊依舊可以使用phaser.register()如果需要減少方框的數量可以調用phaser.arriveAndDeregister(),也就是通知Phaser對象,當前線程已經結束這個階段,并且將不再參與接下來的階段操作。這就相當于說明這個階段到達了方框,并且下一個階段減少一個方框。一般一個線程的所有階段任務都結束了,需要調用phaser.arriveAndDeregister();或者一個線程在執行的時候我們希望停止這個線程的執行了,那么可以調用phaser.arriveAndDeregister();通知phaser后面階段不用同步這個線程了。
Phaser還提供了一個方法protected boolean onAdvance(int phase, int registeredParties),這個方法每當統一放行的時候會自動執行一次,它接收2個參數:當前階段數(從0開始)和注冊的參與者數;
在這里有一個例子,它實現一個模擬測驗,所有學生要完成他們的練習。全部的學生都必須完成同一個練習才能繼續下一個練習。作為一個練習,你可以將例子做個擴展,比如如果解題時間(代碼中表現為線程睡眠時間)大于8s,就說明學生做不出來這一題,則退出測驗。
下面是例子的輸出,一個線程代表一個學生:
Thread-4: Is going to do the first exercise. Mon Jul 22 13:40:50 CST 2019 Thread-4: Has done the first exercise. Mon Jul 22 13:40:50 CST 2019 Thread-3: Is going to do the first exercise. Mon Jul 22 13:40:50 CST 2019 Thread-0: Is going to do the first exercise. Mon Jul 22 13:40:50 CST 2019 Thread-2: Has done the first exercise. Mon Jul 22 13:40:52 CST 2019 Thread-0: Has done the first exercise. Mon Jul 22 13:40:55 CST 2019 Thread-1: Has done the first exercise. Mon Jul 22 13:40:56 CST 2019 Thread-3: Has done the first exercise. Mon Jul 22 13:40:57 CST 2019 Phaser: All the students has finished the first exercise. Phaser: It's turn for the second one. Thread-1: Is going to do the second exercise. Mon Jul 22 13:40:57 CST 2019 Thread-0: Is going to do the second exercise. Mon Jul 22 13:40:57 CST 2019 Thread-2: Is going to do the second exercise. Mon Jul 22 13:40:57 CST 2019 Thread-2: Has done the second exercise. Mon Jul 22 13:40:57 CST 2019 Thread-4: Is going to do the second exercise. Mon Jul 22 13:40:57 CST 2019 Thread-3: Is going to do the second exercise. Mon Jul 22 13:40:57 CST 2019 Thread-0: Has done the second exercise. Mon Jul 22 13:40:59 CST 2019 Thread-4: Has done the second exercise. Mon Jul 22 13:40:59 CST 2019 Thread-3: Has done the second exercise. Mon Jul 22 13:41:03 CST 2019 Thread-1: Has done the second exercise. Mon Jul 22 13:41:04 CST 2019 Phaser: All the students has finished the second exercise. Phaser: It's turn for the third one. Thread-3: Is going to do the third exercise. Mon Jul 22 13:41:04 CST 2019 Thread-3: Has finished the exam. Mon Jul 22 13:41:04 CST 2019 Thread-4: Is going to do the third exercise. Mon Jul 22 13:41:04 CST 2019 Thread-0: Is going to do the third exercise. Mon Jul 22 13:41:04 CST 2019 Thread-2: Is going to do the third exercise. Mon Jul 22 13:41:04 CST 2019 Thread-1: Is going to do the third exercise. Mon Jul 22 13:41:04 CST 2019 Thread-0: Has finished the exam. Mon Jul 22 13:41:06 CST 2019 Thread-4: Has finished the exam. Mon Jul 22 13:41:09 CST 2019 Thread-2: Has finished the exam. Mon Jul 22 13:41:09 CST 2019 Thread-1: Has finished the exam. Mon Jul 22 13:41:09 CST 2019 Phaser: All the students has finished the exam. Phaser: Thank you for your time. Main: The phaser has finished: true.下面是添加了模擬學生不會做的情況的輸出:
Thread-1: Has arrived to do the exam. Mon Jul 22 14:00:09 CST 2019 Thread-4: Has arrived to do the exam. Mon Jul 22 14:00:09 CST 2019 Thread-3: Has arrived to do the exam. Mon Jul 22 14:00:09 CST 2019 Thread-0: Has arrived to do the exam. Mon Jul 22 14:00:09 CST 2019 Thread-2: Has arrived to do the exam. Mon Jul 22 14:00:09 CST 2019 Phaser: The exam are going to start. The students are ready. Phaser: We have 5 students. Thread-2: Is going to do the first exercise. Mon Jul 22 14:00:09 CST 2019 Thread-2: Exit the exam Thread-4: Is going to do the first exercise. Mon Jul 22 14:00:09 CST 2019 Thread-4: Exit the exam Thread-0: Is going to do the first exercise. Mon Jul 22 14:00:09 CST 2019 Thread-0: Has done the first exercise. Mon Jul 22 14:00:09 CST 2019 Thread-3: Is going to do the first exercise. Mon Jul 22 14:00:09 CST 2019 Thread-1: Is going to do the first exercise. Mon Jul 22 14:00:09 CST 2019 Thread-1: Exit the exam Thread-3: Has done the first exercise. Mon Jul 22 14:00:14 CST 2019 Phaser: All the students has finished the first exercise. Phaser: It's turn for the second one. Thread-0: Is going to do the second exercise. Mon Jul 22 14:00:14 CST 2019 Thread-0: Exit the exam Thread-3: Is going to do the second exercise. Mon Jul 22 14:00:14 CST 2019 Thread-3: Has done the second exercise. Mon Jul 22 14:00:19 CST 2019 Phaser: All the students has finished the second exercise. Phaser: It's turn for the third one. Thread-3: Is going to do the third exercise. Mon Jul 22 14:00:19 CST 2019 Thread-3: Exit the exam Phaser: All the students has finished the exam. Phaser: Thank you for your time. Main: The phaser has finished: true.總結
以上是生活随笔為你收集整理的理解java并发工具Phaser的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LinkedHashMap 实现缓存(L
- 下一篇: JMM中的原子性、可见性、有序性和vol