练习5
package zuoYe03; public class Running implements Runnable { // 一个选手跑100米 10个选手才跑1000米 public static int count = 0; @Override public void run() { // TODO Auto-generated method stub synchronized (this) { System.out.println(Thread.currentThread().getName() + "拿到了接力棒!"); for (int i = 1; i <= 10; i++) { System.out.println(Thread.currentThread().getName() + "跑了" + 10 * i + "米!"); } ++count; if (count == 10) { System.out.println("恭喜团队跑满1000米!"); return; } else { System.out.println("还未跑完,团队仍需努力!"); return; } } } }
package zuoYe03; public class Test { public static void main(String[] args) { Running r = new Running(); for (int i = 1; i <= 10; i++) { Thread t = new Thread(r,i+"号选手"); t.start(); } } }
实现结果: