(一)线程管理_4---线程休眠和恢复

来源:互联网 发布:高考物理知乎 编辑:程序博客网 时间:2024/06/09 16:06

线程休眠和恢复

在有的时候需要将线程暂时停止执行,在一段时间后恢复线程,让其继续执行;比如一个线程每隔一分钟执行一次状态检查,那么余下的时间我们希望它能够让出CPU time,什么也不做,最大的减少资源的浪费;在线程恢复后,再给予CPU time,让其继续执行;为了满足这样一个需求,可以调用Thread类的sleep()方法达到这个目的;

Thread.sleep(millis) ,另外还可以使用TimeUnit类进行休眠,如TimeUnit.SECONDS.sleep(millis) ;

动手实验

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();    }}

一次运行结果:

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,已观察执行过程,那么下面开始

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.

显然 sleep语句后面的“After sleep"并没有输出,从输出的结果可以看到子线程也并没有去休眠200毫秒,而至直接中断被catch捕获;

那么可以得出结论:当一个线程的被设置了中断状态,如调用了interrupt(),那么该线程在执行到sleep时,将会立即被中断捕获;


要点

还是要理解线程维护的中断状态和Thread.interrupt()之后线程如何执行;

此外 Thread.yield()方法也能是线程暂时放弃cpu,但是JVM并不保证线程的这个请求的响应;通常,仅仅是作为debug的目的;


0 0
原创粉丝点击