多线程 练习6

时间:2023-02-13 20:58:43

 

 

多线程 练习6

 

package lianxi.demo;

public class RunningThread implements Runnable {
    int m;
    @Override
    public void run() {
        while (m < 1000) {
            running();
        }
    }

    synchronized public void running() {
       if(m<1000){
           //跑100米,每十米显示信息
           Thread t = Thread.currentThread();
           System.out.println(t.getName() + "选手拿到了接力棒!");
           for (int j = 0; j < 10; j++) {
               int num = (j + 1) * 10;
               System.out.println(t.getName() + "选手跑了" + num + "米!");
               m += 10;
           }
           System.out.println("已经跑了" + m + "米!");
       }
    }
}

 

package lianxi.demo;

public class Test {
    public static void main(String[] args) {
        RunningThread r = new RunningThread();
        Thread t1 = new Thread(r);
        Thread t2 = new Thread(r);
        Thread t3 = new Thread(r);
        Thread t4 = new Thread(r);
        Thread t5 = new Thread(r);
        Thread t6 = new Thread(r);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t6.start();
    }
}

多线程 练习6

多线程 练习6

 

 多线程 练习6

多线程 练习6

 

 多线程 练习6

多线程 练习6

多线程 练习6

 

 多线程 练习6

 

多线程 练习6

 

多线程 练习6

 

 

 多线程 练习6