/**
4 * 跑步线程类
5 */
6 public class RunThread implements Runnable {
7 private int meters = 1000; //共跑1000米
8 public void run() {
9 while (true) {
10 if(meters<=100){
11 break;
12 }
13 go();
14 return;
15 }
16 }
17 //同步方法:跑步(每人跑100米)
18 public synchronized void go(){
19 System.out.println(Thread.currentThread().getName()+"拿到接力棒!");
20 for (int i = 0; i < 100; i += 10) {
21 try {
22 Thread.sleep(100);
23 } catch (InterruptedException e) {
24 e.printStackTrace();
25 }
26 System.out.println(Thread.currentThread().getName() + "跑了" + (i + 10)
27 + "米!");
28 }
29 meters-=100;
30
31 }
32 }
public class Test {
9 public static void main(String[] args) {
10 RunThread run = new RunThread();
11 //10人参加接力赛跑
12 for(int i=0;i<10;i++){
13 new Thread(run,(i+1)+"号选手").start();
14 }
15 }
16 }