日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

理解java并发工具Phaser

發布時間:2025/4/16 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 理解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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。