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 }
相关文章: