【线程休眠】

2021-07-12 19:06

阅读:402

标签:extends   override   dex   ide   void   main   system   trace   ring   

package com.test;

import java.util.Date;

public class ThreadSleep extends Thread {

    @Override
    public void run() {
        for (int x = 1; x ) {
            System.out.println(getName() + ":" + x + ",日期:" + new Date());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
package com.test;

public class ThreadSleepTest {

    public static void main(String[] args){

        ThreadSleep ts1 = new ThreadSleep();
        ThreadSleep ts2 = new ThreadSleep();
        ThreadSleep ts3 = new ThreadSleep();

        ts1.setName("线程一");
        ts2.setName("线程二");
        ts3.setName("线程三");

        ts1.start();
        ts2.start();
        ts3.start();

    }

}

 运行结果:

线程一:1,日期:Mon Aug 27 23:06:00 CST 2018
线程二:1,日期:Mon Aug 27 23:06:00 CST 2018
线程三:1,日期:Mon Aug 27 23:06:00 CST 2018
线程一:2,日期:Mon Aug 27 23:06:01 CST 2018
线程二:2,日期:Mon Aug 27 23:06:01 CST 2018
线程三:2,日期:Mon Aug 27 23:06:01 CST 2018
线程一:3,日期:Mon Aug 27 23:06:02 CST 2018
线程二:3,日期:Mon Aug 27 23:06:02 CST 2018
线程三:3,日期:Mon Aug 27 23:06:02 CST 2018
线程一:4,日期:Mon Aug 27 23:06:03 CST 2018
线程二:4,日期:Mon Aug 27 23:06:03 CST 2018
线程三:4,日期:Mon Aug 27 23:06:03 CST 2018
线程一:5,日期:Mon Aug 27 23:06:04 CST 2018
线程二:5,日期:Mon Aug 27 23:06:04 CST 2018
线程三:5,日期:Mon Aug 27 23:06:04 CST 2018
线程一:6,日期:Mon Aug 27 23:06:05 CST 2018
线程二:6,日期:Mon Aug 27 23:06:05 CST 2018
线程三:6,日期:Mon Aug 27 23:06:05 CST 2018
线程一:7,日期:Mon Aug 27 23:06:06 CST 2018
线程二:7,日期:Mon Aug 27 23:06:06 CST 2018
线程三:7,日期:Mon Aug 27 23:06:06 CST 2018
线程一:8,日期:Mon Aug 27 23:06:07 CST 2018
线程二:8,日期:Mon Aug 27 23:06:07 CST 2018
线程三:8,日期:Mon Aug 27 23:06:07 CST 2018
线程一:9,日期:Mon Aug 27 23:06:08 CST 2018
线程二:9,日期:Mon Aug 27 23:06:08 CST 2018
线程三:9,日期:Mon Aug 27 23:06:08 CST 2018

 

【线程休眠】

标签:extends   override   dex   ide   void   main   system   trace   ring   

原文地址:https://www.cnblogs.com/zuixinxian/p/9545448.html


评论


亲,登录后才可以留言!