码迷,mamicode.com
首页 > 编程语言 > 详细

【线程休眠】

时间:2018-08-28 00:58:25      阅读:174      评论:0      收藏:0      [点我收藏+]

标签: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 < 10; 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!