多线程---模拟接力赛跑

 1 //接力赛跑的线程,实现Runnable
 2 public class MyRunnable implements Runnable{
 3     static int num = 10;
 4     private String thread;
 5     Object lock = new Object();
 6     public void run() {
 7         while(true) {
 8             synchronized (lock) {
 9                 if(num == 0) {
10                     System.out.println("比赛结束!");
11                     break;
12                 }
13                 thread = Thread.currentThread().getName();
14                 System.out.println(thread+"拿到了接力棒!");
15                 num--;            
16                 for(int i =1;i<=10;i++) {
17                     try {
18                         Thread.sleep(10);
19                     } catch (InterruptedException e) {
20                         // TODO Auto-generated catch block
21                         e.printStackTrace();
22                     }
23                     System.out.println(thread+"跑了"+(i*10));
24                 }
25                 return;
26             }
27         }
28     }
29 }
接力赛跑的线程,实现Runnable

相关文章:

  • 2022-12-23
  • 2021-07-07
  • 2021-11-20
  • 2021-05-20
  • 2021-08-01
  • 2022-12-23
  • 2021-11-07
  • 2021-11-19
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
  • 2021-11-20
  • 2021-12-05
  • 2022-01-05
相关资源
相似解决方案