线程休眠

线程休眠:

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

你可能感兴趣的:(线程休眠)