Thread.interrupt()方法

本文深入探讨了Java线程中断方法的使用,包括Thread.interrupt()、Thread.sleep()、Thread.join()等方法的中断机制及应用案例。
在JDK1.0中,可以用stop方法来终止,但是现在这种方法已经被禁用了,改用interrupt方法。


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


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


{...}


而被block的线程(sleep() or join())在被调用interrupt时会产生InterruptException,此时是否终止线程由本线程自己决定。程序的一般形式是:
public void run()
{
try
{
. . .
while (!Thread.currentThread().isInterrupted() && more work to do)
{
do more work
}
}
catch(InterruptedException e)
{
// thread was interrupted during sleep or wait
}
finally
{
cleanup, if required
}
// exiting the run method terminates the thread
}


Thread.sleep方法也会产生InterruptedException,因此,如果每次在做完一些工作后调用了sleep方法,那么就不用检查isInterrupted,而是直接捕捉InterruptedException。


---------------------------------------------------------------------------------------


假如我们有一个任务如下,交给一个Java线程来执行,如何才能保证调用interrupt()来中断它呢?


class ATask implements Runnable{  
  
    private double d = 0.0;  
      
    public void run() {  
        //死循环执行打印"I am running!" 和做消耗时间的浮点计算  
        while (true) {  
            System.out.println("I am running!");  
              
            for (int i = 0; i < 900000; i++) {  
                d =  d + (Math.PI + Math.E) / d;  
            }  
            //给线程调度器可以切换到其它进程的信号  
            Thread.yield();  
        }  
    }  
}  
  
public class InterruptTaskTest {  
      
    public static void main(String[] args) throws Exception{  
        //将任务交给一个线程执行  
        Thread t = new Thread(new ATask());  
        t.start();  
          
        //运行一断时间中断线程  
        Thread.sleep(100);  
        System.out.println("****************************");  
        System.out.println("Interrupted Thread!");  
        System.out.println("****************************");  
        t.interrupt();  
    }  
}   




运行这个程序,我们发现调用interrupt()后,程序仍在运行,如果不强制结束,程序将一直运行下去,如下所示: 
......  
I am running!  
I am running!  
I am running!  
I am running!  
****************************  
Interrupted Thread!  
****************************  
I am running!  
I am running!  
I am running!  
I am running!  
I am running!  
....  


虽然中断发生了,但线程仍然在进行,离开线程有两种常用的方法: 
抛出InterruptedException和用Thread.interrupted()检查是否发生中断,下面分别看一下这两种方法: 
1.在阻塞操作时如Thread.sleep()时被中断会抛出InterruptedException(注意,进行不能中断的IO操作而阻塞和要获得对象的锁调用对象的synchronized方法而阻塞时不会抛出InterruptedException) 
Java代码 
class ATask implements Runnable{  
  
    private double d = 0.0;  
      
    public void run() {  
        //死循环执行打印"I am running!" 和做消耗时间的浮点计算  
        try {  
            while (true) {  
                System.out.println("I am running!");  
                  
                for (int i = 0; i < 900000; i++) {  
                    d =  d + (Math.PI + Math.E) / d;  
                }  
                //休眠一断时间,中断时会抛出InterruptedException  
                Thread.sleep(50);  
            }  
        } catch (InterruptedException e) {  
            System.out.println("ATask.run() interrupted!");  
        }  
    }  
}  


程序运行结果如下: 
Java代码 
I am running!  
I am running!  
****************************  
Interrupted Thread!  
****************************  
ATask.run() interrupted!  


可以看到中断任务时让任务抛出InterruptedException来离开任务. 


2.Thread.interrupted()检查是否发生中断.Thread.interrupted()能告诉你线程是否发生中断,并将清除中断状态标记,所以程序不会两次通知你线程发生了中断. 
Java代码 
class ATask implements Runnable{  
  
    private double d = 0.0;  
      
    public void run() {  
          
        //检查程序是否发生中断  
        while (!Thread.interrupted()) {  
            System.out.println("I am running!");  
  
            for (int i = 0; i < 900000; i++) {  
                d = d + (Math.PI + Math.E) / d;  
            }  
        }  
  
        System.out.println("ATask.run() interrupted!");  
    }  
}  


程序运行结果如下: 
Java代码 
I am running!  
I am running!  
I am running!  
I am running!  
I am running!  
I am running!  
I am running!  
****************************  
Interrupted Thread!  
****************************  
ATask.run() interrupted!  




我们可结合使用两种方法来达到可以通过interrupt()中断线程.请看下面例子: 
Java代码 
class ATask implements Runnable{  
  
    private double d = 0.0;  
      
    public void run() {  
          
        try {  
        //检查程序是否发生中断  
        while (!Thread.interrupted()) {  
            System.out.println("I am running!");  
            //point1 before sleep  
            Thread.sleep(20);  
            //point2 after sleep  
            System.out.println("Calculating");  
            for (int i = 0; i < 900000; i++) {  
                d = d + (Math.PI + Math.E) / d;  
            }  
        }  
          
        } catch (InterruptedException e) {  
            System.out.println("Exiting by Exception");  
        }  
          
        System.out.println("ATask.run() interrupted!");  
    }  
}  


在point1之前处point2之后发生中断会产生两种不同的结果,可以通过修改InterruptTaskTest main()里的Thread.sleep()的时间来达到在point1之前产生中断或在point2之后产生中断. 
如果在point1之前发生中断,程序会在调用Thread.sleep()时抛出InterruptedException从而结束线程.这和在Thread.sleep()时被中断是一样的效果.程序运行结果可能如下:
Java代码 
I am running!  
Calculating  
I am running!  
Calculating  
I am running!  
Calculating  
I am running!  
****************************  
Interrupted Thread!  
****************************  
Exiting by Exception  
ATask.run() interrupted!  


如果在point2之后发生中断,线程会继续执行到下一次while判断中断状态时.程序运行结果可能如下: 
Java代码 
I am running!  
Calculating  
I am running!  
Calculating  
I am running!  
Calculating  
****************************  
Interrupted Thread!  
****************************  
ATask.run() interrupted!  














Java多线程sleep(),join(),interrupt(),wait(),notify()
关于Java多线程知识可以看看《Thinking in Java 》中的多线程部分和《Java网络编程》中第5章多线程的部分


以下是参考<<Java多线程模式>>的 
1. sleep() & interrupt() 
    线程A正在使用sleep()暂停着: Thread.sleep(100000); 
    如果要取消他的等待状态,可以在正在执行的线程里(比如这里是B)调用 
        a.interrupt(); 
    令线程A放弃睡眠操作,这里a是线程A对应到的Thread实例 
    执行interrupt()时,并不需要获取Thread实例的锁定.任何线程在任何时刻,都可以调用其他线程interrupt().当sleep中的线程被调用interrupt()时,就会放弃暂停的状态.并抛出InterruptedException.丢出异常的,是A线程. 


2. wait() & interrupt() 
    线程A调用了wait()进入了等待状态,也可以用interrupt()取消. 
    不过这时候要小心锁定的问题.线程在进入等待区,会把锁定解除,当对等待中的线程调用interrupt()时(注意是等待的线程调用其自己的interrupt()),会先重新获取锁定,再抛出异常.在获取锁定之前,是无法抛出异常的. 


3. join() & interrupt() 
    当线程以join()等待其他线程结束时,一样可以使用interrupt()取消之.因为调用join()不需要获取锁定,故与sleep()时一样,会马上跳到catch块里. 注意是随调用interrupt()方法,一定是阻塞的线程来调用其自己的interrupt方法.如在线程a中调用来线程t.join().则a会等t执行完后在执行t.join后的代码,当在线程b中调用来a.interrupt()方法,则会抛出InterruptedException


4. interrupt()只是改变中断状态而已 
    interrupt()不会中断一个正在运行的线程。这一方法实际上完成的是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。更确切的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。 
    如果线程没有被阻塞,这时调用interrupt()将不起作用;否则,线程就将得到异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。 
    线程A在执行sleep,wait,join时,线程B调用A的interrupt方法,的确这一个时候A会有InterruptedException异常抛出来.但这其实是在sleep,wait,join这些方法内部会不断检查中断状态的值,而自己抛出的InterruptedException。 
    如果线程A正在执行一些指定的操作时如赋值,for,while,if,调用方法等,都不会去检查中断状态,所以线程A不会抛出InterruptedException,而会一直执行着自己的操作.当线程A终于执行到wait(),sleep(),join()时,才马上会抛出InterruptedException. 
    若没有调用sleep(),wait(),join()这些方法,或是没有在线程里自己检查中断状态自己抛出InterruptedException的话,那InterruptedException是不会被抛出来的. 


顺便加个与Thread.sleep()相同效果的代码: 
public static void amethod(long x) throws InterruptedExcetion{ 
    if (x != 0) { 
        Object o = new Object(); 
        synchronized (o) { 
            o.wait(x); 
        } 
    } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值