黑马程序员技术交流社区

标题: 线程等待与唤醒的经典小例子 [打印本页]

作者: 花海1989    时间: 2013-9-5 21:06
标题: 线程等待与唤醒的经典小例子
线程的休眠与唤醒
复制代码 代码如下:

Thread.sleep实例
public class SleepSample {

     public static void main(String[] args) throws InterruptedException
     {
         sleepTest();
     }

     private static void sleepTest() throws InterruptedException
     {
         Thread thread = new Thread()
         {
             public void run()
             {
                 System.out.println("线程 " + Thread.currentThread().getName() + "将要休眠5分钟。");
                 try
                 {
                     Thread.sleep(5*60*1000);
                 }
                 catch(InterruptedException ex)
                 {
                     System.out.println("线程 " + Thread.currentThread().getName() + "休眠被中断。");
                 }
                 System.out.println("线程 " + Thread.currentThread().getName() + "休眠结束。");
             }
         };
         thread.setDaemon(true);
         thread.start();
         Thread.sleep(500);
         thread.interrupt();
     }

}







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2