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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

等待多个并发事件完成的模型

發布時間:2025/5/22 编程问答 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 等待多个并发事件完成的模型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡單的一個常見問題:如下 一個人刷牙3分鐘,洗臉1分鐘,梳頭1分鐘,煮雞蛋5分鐘。完成這些事情最少多少時間? 這其實對應編程來說就對應了題目的問題了,如何讓主線程計算出多個并發事件完成的時間問題了。 我們下面會接觸一個java類,那就是CountDownLatch 類,詳細內容后面有時間詳細添加,先下面給出一個案例代碼。 下面代碼只創建2個任務線程,計算完成任務最少的時間。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
package test;import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.CountDownLatch;public class Test { public static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) { long start =new Date().getTime(); CountDownLatch latch=new CountDownLatch(2);//兩個任務 DoSomething DoSomething1=new DoSomething("刷牙", 3000, latch);//模擬3秒代表3分鐘 DoSomething DoSomething2=new DoSomething("煮雞蛋", 5000, latch);//模擬5秒代表5分鐘 DoSomething1.start(); DoSomething2.start(); try { latch.await(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }//等待所有任務完成 System.out.println("總用時: "+(new Date().getTime()-start)/1000 +"秒"); } public static class DoSomething extends Thread{ String jobName; int needTime; CountDownLatch latch; public DoSomething(String jobName ,int needTime ,CountDownLatch latch){ this.jobName=jobName; this.needTime=needTime; this.latch=latch; } public void run(){ System.out.println(sdf.format(new Date())+": "+jobName+"開始"); try { Thread.sleep(needTime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ System.out.println(sdf.format(new Date())+": "+jobName+"結束"); latch.countDown();//一個任務完成 } } } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

?

http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244545
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244541
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244538
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244527
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244528
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244529
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244530

轉載于:https://www.cnblogs.com/sy646et/p/7266031.html

總結

以上是生活随笔為你收集整理的等待多个并发事件完成的模型的全部內容,希望文章能夠幫你解決所遇到的問題。

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