Java Thread.sleep的InterruptedException

来源:互联网 发布:施工计划软件 编辑:程序博客网 时间:2024/06/05 05:55

1.1.1  Thread.sleep抛异常

当某线程A处于Sleep状态时,另一个线程B调用了B.interrupt()方法,打断了A的Sleep过程,则A的Sleep会抛出异常。使用Catch后,线程不会等待Sleep时间,而是会立即执行。

 

下面的例子中,线程1的Sleep会被线程2打断,所以线程1的Sleep会抛异常。

 

public class SleepException {

    public static void main(String[] args) {      

        final Threadt1 = new Thread(){ 

           public void run() {

            System.out.println("Thread1 start!");

            while (true){

                System.out.println("Thread1 sleep start!");

                    try

                        Thread.sleep(10000); 

                    } catch (InterruptedExceptione) { 

                      System.out.println("Thread1 sleep exception e="+e.getMessage());

                        e.printStackTrace(); 

                    }

            }

           } 

       }; 

       Thread t2 = new Thread() { 

           public void run() {

            System.out.println("Thread2 start!");

            while (true){

                System.out.println("Thread2 sleep start!");

                    try

                        Thread.sleep(2000); 

                    } catch (InterruptedExceptione) {

                      System.out.println("Thread2 sleep exception e="+e);

                        e.printStackTrace(); 

                    } 

                    t1.interrupt();

            }

           } 

       }; 

        t1.start(); 

        t2.start(); 

    } 

}

1.1.2  Thread.interrupt的中断信号

Thread.interrupt()方法不会中断一个正在运行的线程。它的作用是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。更确切的说,如果线程被Object.wait,Thread.joinThread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。

interrupt方法并不是强制终止线程,它只能设置线程的interrupted状态,而在线程中一般使用一下方式:
while (!Thread.currentThread().isInterrupted() && more work to do)

{...}

 

1.        调用A.interrupt时,会设置A的isInterrupted状态,通知A有重要的事情需要处理了。

2.        具体A怎么处理,取决于A自己。也许是退出,也可以继续执行。

3.        如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。以便及时对isInterrupted状态进行处理。

4.        Thread.interrupted()方法可以判断本线程是否收到了中断信号,并进行处理。此方法是读清的,下一次再判断就为false。直到收到新的中断信号。

5.        Thread.currentThread().isInterrupted()方法是非读清的。

6.        中断方法interrupt是异步的,有一定延时的。A程序对B程序中断,A程序在走了几行后,B才能感知到中断。

例子:

public class SleepException {

    public static void main(String[] args) {

        final Threadt1 = new Thread() {

            public void run() {

                System.out.println("Thread1 start!");

                while (true) {

                    if (Thread.currentThread().isInterrupted()){

                        System.out.println("Thread1 isInterrupted 1");

                    } else {

                        // System.out.println("Thread1isInterrupted 1 false");

                    }

                    if (Thread.currentThread().isInterrupted()){

                        System.out.println("Thread1 isInterrupted 2");

                    }

                    if (Thread.interrupted()) {

                        System.out.println("Thread1 interrupted 3");

                    }

                    if (Thread.interrupted()) {

                        System.out.println("Thread1 interrupted 4");

                    }

                    if (Thread.currentThread().isInterrupted()){

                        System.out.println("Thread1 isInterrupted 5");

                    }

                }

            }

        };

        Thread t2 = new Thread() {

            public void run() {

                System.out.println("Thread2 start!");

                while (true) {

                    System.out

                            .println("=================Thread2sleep start===============!");

                    try {

                        Thread.sleep(4000);

                    } catch (InterruptedExceptione) {

                        System.out.println("Thread2 sleep exception e=" +e);

                        e.printStackTrace();

                    }

                    t1.interrupt();

                }

            }

        };

        t1.start();

        t2.start();

    }

}
原创粉丝点击