多執行緒 模擬接力賽跑

2022-08-22 19:00:10 字數 790 閱讀 9581

//接力賽跑的執行緒,實現runnable

2public

class myrunnable implements

runnable

13 thread =thread.currentthread().getname();

14 system.out.println(thread+"拿到了接力棒!");

15 num--;

16for(int i =1;i<=10;i++) catch

(interruptedexception e)

23 system.out.println(thread+"跑了"+(i*10));24}

25return;26

}27}28

}29 }

接力賽跑的執行緒,實現runnable

1

//測試類

2public

class

test

27 }

測試類執行結果:

問題:無法實現少於十人的接力賽跑,實現不連續出現的情況!

205 接力賽跑

題目描述 n只奶牛 編號為1 n 在進行乙個特別的接力賽跑,這個比賽特別之處在於,若干只奶牛可以同時跑。在t 0時刻,牛1開始沿著跑道跑,l 1秒後跑完一圈重新到達起點線。通常,牛i跑完一圈需要的時間為l i秒。當牛1重新越過起點線的瞬間,它會通知另外m 1只奶牛立即起跑。通常,牛i會通知另外m i...

多執行緒模擬龜兔賽跑

首先來個賽道距離,然後要離終點越來越近 判斷比賽是否結束 列印出勝利者 龜兔賽跑開始 故事中是烏龜贏的,兔子需要睡覺,所以我們來模擬兔子睡覺 終於,烏龜贏得比賽 package com.weather.thread 模擬龜兔賽跑 public class race implements runnab...

多執行緒之賽跑 CountDownLatch

countdownlatch 等所有的執行緒準備好之後發起執行後續流程.如下 author jeremy.li date 2020 11 30 description public class countdownlatchtest implements runnable catch interrup...