使用CountDownLatch来模拟马拉松比赛
生活随笔
收集整理的這篇文章主要介紹了
使用CountDownLatch来模拟马拉松比赛
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
import?java.util.Queue; import?java.util.Random; import?java.util.concurrent.ConcurrentLinkedQueue; import?java.util.concurrent.CountDownLatch; import?java.util.concurrent.ExecutorService; import?java.util.concurrent.Executors;/****?使用CountDownLatch來模擬馬拉松比賽**?@author?楊尚川*/ public?class?MarathonSimulator?{public?static?void?main(String[]?args)?throws?Exception{ExecutorService?executorService?=?Executors.newCachedThreadPool();int?personCount?=?3;final?CountDownLatch?prepare?=?new?CountDownLatch(personCount);final?CountDownLatch?start?=?new?CountDownLatch(1);final?CountDownLatch?end?=?new?CountDownLatch(personCount);final?Queue<String>?queue?=?new?ConcurrentLinkedQueue<>();if(personCount?<?3){System.out.println("參賽人數(shù)必須?>?2");return;}for(int?i=1;?i<=personCount;?i++){final?int?personNumber?=?i;executorService.submit(new?Runnable()?{@Overridepublic?void?run()?{//模擬準(zhǔn)備時(shí)間,使用隨機(jī)數(shù)try{Thread.sleep(new?Random(personNumber).nextInt(1000));}catch(Exception?e){}System.out.println(personNumber+"號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢");prepare.countDown();try?{//等待開始跑的命令start.await();}catch?(Exception?e){}System.out.println(personNumber+"號(hào)運(yùn)動(dòng)員開始跑");long?s?=?System.currentTimeMillis();//模擬跑步時(shí)間,使用隨機(jī)數(shù)try{Thread.sleep(new?Random(personNumber).nextInt(10000));}catch(Exception?e){}long?c?=?System.currentTimeMillis()?-?s;System.out.println(personNumber+"號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):"+c+"毫秒");end.countDown();queue.add(personNumber+"號(hào)");}});}System.out.println("有"+personCount+"名運(yùn)動(dòng)員參加本次的馬拉松比賽");System.out.println("教練在等待運(yùn)動(dòng)員準(zhǔn)備...\n");prepare.await();System.out.println("\n所有運(yùn)動(dòng)員準(zhǔn)備完畢,教練開始喊:預(yù)備?---?跑!");start.countDown();System.out.println("\n教練在等待所有的運(yùn)動(dòng)員跑完...\n");end.await();System.out.println("\n所有運(yùn)動(dòng)員都跑完了,開始頒獎(jiǎng)啦:");System.out.println("冠軍是:"+queue.poll());System.out.println("亞軍是:"+queue.poll());System.out.println("季軍是:"+queue.poll());executorService.shutdown();} } 10人參加比賽程序運(yùn)行之后的輸出結(jié)果:有10名運(yùn)動(dòng)員參加本次的馬拉松比賽 教練在等待運(yùn)動(dòng)員準(zhǔn)備...2號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢 10號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢 7號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢 8號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢 5號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢 6號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢 3號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢 4號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢 1號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢 9號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢所有運(yùn)動(dòng)員準(zhǔn)備完畢,教練開始喊:預(yù)備?---?跑!教練在等待所有的運(yùn)動(dòng)員跑完...2號(hào)運(yùn)動(dòng)員開始跑 7號(hào)運(yùn)動(dòng)員開始跑 3號(hào)運(yùn)動(dòng)員開始跑 4號(hào)運(yùn)動(dòng)員開始跑 9號(hào)運(yùn)動(dòng)員開始跑 10號(hào)運(yùn)動(dòng)員開始跑 1號(hào)運(yùn)動(dòng)員開始跑 6號(hào)運(yùn)動(dòng)員開始跑 5號(hào)運(yùn)動(dòng)員開始跑 8號(hào)運(yùn)動(dòng)員開始跑 4號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):1866毫秒 8號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):2367毫秒 3號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):3736毫秒 7號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):4241毫秒 2號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):6113毫秒 6號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):6616毫秒 10號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):7117毫秒 1號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):8989毫秒 5號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):9491毫秒 9號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):9993毫秒所有運(yùn)動(dòng)員都跑完了,開始頒獎(jiǎng)啦: 冠軍是:4號(hào) 亞軍是:8號(hào) 季軍是:3號(hào) 3人參加比賽程序運(yùn)行之后的輸出結(jié)果:有3名運(yùn)動(dòng)員參加本次的馬拉松比賽 教練在等待運(yùn)動(dòng)員準(zhǔn)備...2號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢 3號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢 1號(hào)運(yùn)動(dòng)員準(zhǔn)備完畢所有運(yùn)動(dòng)員準(zhǔn)備完畢,教練開始喊:預(yù)備?---?跑!教練在等待所有的運(yùn)動(dòng)員跑完...2號(hào)運(yùn)動(dòng)員開始跑 1號(hào)運(yùn)動(dòng)員開始跑 3號(hào)運(yùn)動(dòng)員開始跑 3號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):3737毫秒 2號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):6112毫秒 1號(hào)運(yùn)動(dòng)員到達(dá)目的地,耗時(shí):8990毫秒所有運(yùn)動(dòng)員都跑完了,開始頒獎(jiǎng)啦: 冠軍是:3號(hào) 亞軍是:2號(hào) 季軍是:1號(hào)轉(zhuǎn)載于:https://my.oschina.net/apdplat/blog/385448
總結(jié)
以上是生活随笔為你收集整理的使用CountDownLatch来模拟马拉松比赛的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5分钟在超能云(SuperVessel)
- 下一篇: Hadoop集群中添加硬盘