线程休眠

来源:互联网 发布:上海8院811所知乎 编辑:程序博客网 时间:2024/05/17 22:16

线程休眠:

public staticvoid sleep(long millis)

import java.util.Date;

 

public classThreadSleep extends Thread {

   @Override

   public void run() {

      for (int x = 0; x < 100; x++){

         System.out.println(getName() +":" + x + ",日期:" +new Date());

         // 睡眠

         // 困了,我稍微休息1秒钟

         try {

            Thread.sleep(1000);

         }catch(InterruptedException e) {

            e.printStackTrace();

         }

      }

   }

}

 

/*

 * 线程休眠

 *    publicstatic void sleep(long millis)

 */

public classThreadSleepDemo {

   public static void main(String[] args) {

      ThreadSleepts1 = newThreadSleep();

      ThreadSleepts2 = newThreadSleep();

      ThreadSleepts3 = newThreadSleep();

 

      ts1.setName("线程一");

      ts2.setName("线程二");

      ts3.setName("线程三");

 

      ts1.start();

      ts2.start();

      ts3.start();

   }

}

运行结果:

线程一:0,日期:Tue May 03 20:14:20 CST 2016

线程二:0,日期:Tue May 03 20:14:20 CST 2016

线程三:0,日期:Tue May 03 20:14:20 CST 2016

线程三:1,日期:Tue May 03 20:14:21 CST 2016

线程二:1,日期:Tue May 03 20:14:21 CST 2016

线程一:1,日期:Tue May 03 20:14:21 CST 2016

线程二:2,日期:Tue May 03 20:14:22 CST 2016

0 0
原创粉丝点击