在有的时候须要将线程暂时中止执行,在一段时间后恢复线程,让其继续执行;好比一个线程每隔一分钟执行一次状态检查,那么余下的时间咱们但愿它可以让出CPU time,什么也不作,最大的减小资源的浪费;在线程恢复后,再给予CPU time,让其继续执行;为了知足这样一个需求,能够调用Thread类的sleep()方法达到这个目的;java
Thread.sleep(millis) ,另外还可使用TimeUnit类进行休眠,如TimeUnit.SECONDS.sleep(millis) ;ide
public class FileClock implements Runnable{ @Override public void run() { /* 每次循环打印一个时间,模拟一个任务,以后休眠1毫秒, 在sleep时,若是被中断,将会抛出InterruptedException */ for (int i = 0; i < 10; i++) { System.out.printf("%s\n",new Date()); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { System.out.printf("The FileClock has been interrupted."); } } } public static void main(String[] args) { Thread thread = new Thread(new FileClock()); thread.start(); /* 主线程等待5毫秒 */ try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } /* 唤醒FileClock线程 */ thread.interrupt(); } }
一次运行结果:spa
Thu Oct 30 23:09:25 CST 2014
Thu Oct 30 23:09:26 CST 2014
Thu Oct 30 23:09:27 CST 2014
Thu Oct 30 23:09:28 CST 2014
Thu Oct 30 23:09:29 CST 2014
The FileClock has been interrupted.Thu Oct 30 23:09:30 CST 2014
Thu Oct 30 23:09:31 CST 2014
Thu Oct 30 23:09:32 CST 2014
Thu Oct 30 23:09:33 CST 2014
Thu Oct 30 23:09:34 CST 2014
线程
首先,fileclock线程开始执行,而后主线程等待,在fileclock还没结束,主线程执行对fileclock执行了interrupt()方法,注意这里,上节记录的这个方法会有effect以及它和IsInterrupt的区别,这个方法设置线程的中断标记,那么此时有一个问题:若是此时fileclock刚尚未进入sleep方法,此时该怎么执行?会不会当再次进入sleep时直接中断进入catch块,仍是执行休眠完后中断? 若是此时fileclock线程正在休眠,显然会直接中断被catch块捕获,这个就毫无疑问了,那么下面就上面这个疑惑进行一个实验,主线程启动子线程后,调用interrupt,设置子线程已经中断,故意让子线程必定在主线程执行了interrupt以后,子线程才调用sleep,已观察执行过程,那么下面开始debug
public class InterruptTest implements Runnable{ @Override public void run() { /* while的目的是保证在主线执行完thread.interrupt()方法后,才结束while 执行下面的sleep,能够经过结果的两个输出看出(Before,After) */ long start=System.currentTimeMillis(); while((System.currentTimeMillis()-start)<5000){ //empty } System.out.printf("After:Thread will be sleep.\n"); try { TimeUnit.SECONDS.sleep(200); System.out.printf("After sleep.\n"); } catch (InterruptedException e) { System.out.printf("Interrupted."); } } public static void main(String[] args) { Thread thread = new Thread(new InterruptTest()); thread.start(); try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } thread.interrupt(); System.out.printf("Before:Main execute thread.interrupt().\n"); } }执行结果:
Before:Main execute thread.interrupt().
After:Thread will be sleep.
Interrupted.
code
显然 sleep语句后面的“After sleep"并无输出,从输出的结果能够看到子线程也并无去休眠200毫秒,而至直接中断被catch捕获;资源
那么能够得出结论:当一个线程的被设置了中断状态,如调用了interrupt(),那么该线程在执行到sleep时,将会当即被中断捕获;it
仍是要理解线程维护的中断状态和Thread.interrupt()以后线程如何执行;io
此外 Thread.yield()方法也能是线程暂时放弃cpu,可是JVM并不保证线程的这个请求的响应;一般,仅仅是做为debug的目的;class