如何中断线程

线程的thread.interrupt()方法是中断线程,将会设置该线程的中断状态位,即设置为true,中断的结果线程是死亡、还是等待新的任务或是继续运行至下一步,就取决于这个程序本身。线程会不时地检测这个中断标示位,以判断线程是否应该被中断(中断标示值是否为true)。它并不像stop方法那样会中断一个正在运行的线程。

判断线程是否被中断

判断某个线程是否已被发送过中断请求,请使用Thread.currentThread().isInterrupted()方法(因为它将线程中断标示位设置为true后,不会立刻清除中断标示位,即不会将中断标设置为false),而不要使用thread.interrupted()(该方法调用后会将中断标示位清除,即重新设置为false)方法来判断,下面是线程在循环中时的中断方式:

Java代码

  1. while(!Thread.currentThread().isInterrupted()&&moreworktodo){
  2. domorework
  3. }

while(!Thread.currentThread().isInterrupted() && more work to do){

do more work

}

如何中断线程

如果一个线程处于了阻塞状态(如线程调用了thread.sleepthread.jointhread.wait1.5中的condition.await、以及可中断的通道上的 I/O 操作方法后可进入阻塞状态),则在线程在检查中断标示时如果发现中断标示为true,则会在这些阻塞方法(sleepjoinwait1.5中的condition.await及可中断的通道上的 I/O 操作方法)调用处抛出InterruptedException异常,并且在抛出异常后立即将线程的中断标示位清除,即重新设置为false。抛出异常是为了线程从阻塞状态醒过来,并在结束线程前让程序员有足够的时间来处理中断请求。

注,synchronized在获锁的过程中是不能被中断的,意思是说如果产生了死锁,则不可能被中断(请参考后面的测试例子)。与synchronized功能相似的reentrantLock.lock()方法也是一样,它也不可中断的,即如果发生死锁,那么reentrantLock.lock()方法无法终止,如果调用时被阻塞,则它一直阻塞到它获取到锁为止。但是如果调用带超时的tryLock方法reentrantLock.tryLock(long timeout, TimeUnit unit),那么如果线程在等待时被中断,将抛出一个InterruptedException异常,这是一个非常有用的特性,因为它允许程序打破死锁。你也可以调用reentrantLock.lockInterruptibly()方法,它就相当于一个超时设为无限的tryLock方法。

没有任何语言方面的需求一个被中断的线程应该终止。中断一个线程只是为了引起该线程的注意,被中断线程可以决定如何应对中断。某些线程非常重要,以至于它们应该不理会中断,而是在处理完抛出的异常之后继续执行,但是更普遍的情况是,一个线程将把中断看作一个终止请求,这种线程的run方法遵循如下形式:

Java代码

  1. publicvoidrun(){
  2. try{
  3. ...
  4. /*
  5. *不管循环里是否调用过线程阻塞的方法如sleepjoinwait,这里还是需要加上
  6. *!Thread.currentThread().isInterrupted()条件,虽然抛出异常后退出了循环,显
  7. *得用阻塞的情况下是多余的,但如果调用了阻塞方法但没有阻塞时,这样会更安全、更及时。
  8. */
  9. while(!Thread.currentThread().isInterrupted()&&moreworktodo){
  10. domorework
  11. }
  12. }catch(InterruptedExceptione){
  13. //线程在waitsleep期间被中断了
  14. }finally{
  15. //线程结束前做一些清理工作
  16. }
  17. }

public void run() {

try {

...

/*

* 不管循环里是否调用过线程阻塞的方法如sleepjoinwait,这里还是需要加上

* !Thread.currentThread().isInterrupted()条件,虽然抛出异常后退出了循环,显

* 得用阻塞的情况下是多余的,但如果调用了阻塞方法但没有阻塞时,这样会更安全、更及时。

*/

while (!Thread.currentThread().isInterrupted()&& more work to do) {

do more work

}

} catch (InterruptedException e) {

//线程在waitsleep期间被中断了

} finally {

//线程结束前做一些清理工作

}

}

上面是while循环在try块里,如果trywhile循环里时,因该在catch块里重新设置一下中断标示,因为抛出InterruptedException异常后,中断标示位会自动清除,此时应该这样:

Java代码

  1. publicvoidrun(){
  2. while(!Thread.currentThread().isInterrupted()&&moreworktodo){
  3. try{
  4. ...
  5. sleep(delay);
  6. }catch(InterruptedExceptione){
  7. Thread.currentThread().interrupt();//重新设置中断标示
  8. }
  9. }
  10. }

public void run() {

while (!Thread.currentThread().isInterrupted()&& more work to do) {

try {

...

sleep(delay);

} catch (InterruptedException e) {

Thread.currentThread().interrupt();//重新设置中断标示

}

}

}

底层中断异常处理方式

另外不要在你的底层代码里捕获InterruptedException异常后不处理,会处理不当,如下:

Java代码

  1. voidmySubTask(){
  2. ...
  3. try{
  4. sleep(delay);
  5. }catch(InterruptedExceptione){}//不要这样做
  6. ...
  7. }

void mySubTask(){

...

try{

sleep(delay);

}catch(InterruptedException e){}//不要这样做

...

}

如果你不知道抛InterruptedException异常后如何处理,那么你有如下好的建议处理方式:
1
、在catch子句中,调用Thread.currentThread.interrupt()来设置中断状态(因为抛出异常后中断标示会被清除),让外界通过判断Thread.currentThread().isInterrupted()标示来决定是否终止线程还是继续下去,应该这样做:

Java代码

  1. voidmySubTask(){
  2. ...
  3. try{
  4. sleep(delay);
  5. }catch(InterruptedExceptione){
  6. Thread.currentThread().isInterrupted();
  7. }
  8. ...
  9. }

void mySubTask() {

...

try {

sleep(delay);

} catch (InterruptedException e) {

Thread.currentThread().isInterrupted();

}

...

}

2、或者,更好的做法就是,不使用try来捕获这样的异常,让方法直接抛出:

Java代码

  1. voidmySubTask()throwsInterruptedException{
  2. ...
  3. sleep(delay);
  4. ...
  5. }

void mySubTask() throws InterruptedException {

...

sleep(delay);

...

}

中断应用

使用中断信号量中断非阻塞状态的线程

中断线程最好的,最受推荐的方式是,使用共享变量(shared variable)发出信号,告诉线程必须停止正在运行的任务。线程必须周期性的核查这一变量,然后有秩序地中止任务。Example2描述了这一方式:

Java代码

  1. classExample2extendsThread{
  2. volatilebooleanstop=false;//线程中断信号量
  3. publicstaticvoidmain(Stringargs[])throwsException{
  4. Example2thread=newExample2();
  5. System.out.println("Startingthread...");
  6. thread.start();
  7. Thread.sleep(3000);
  8. System.out.println("Askingthreadtostop...");
  9. //设置中断信号量
  10. thread.stop=true;
  11. Thread.sleep(3000);
  12. System.out.println("Stoppingapplication...");
  13. }
  14. publicvoidrun(){
  15. //每隔一秒检测一下中断信号量
  16. while(!stop){
  17. System.out.println("Threadisrunning...");
  18. longtime=System.currentTimeMillis();
  19. /*
  20. *使用while循环模拟sleep方法,这里不要使用sleep,否则在阻塞时会
  21. *InterruptedException异常而退出循环,这样while检测stop条件就不会执行,
  22. *失去了意义。
  23. */
  24. while((System.currentTimeMillis()-time<1000)){}
  25. }
  26. System.out.println("Threadexitingunderrequest...");
  27. }
  28. }

class Example2 extends Thread {

volatile boolean stop = false;// 线程中断信号量

public static void main(String args[]) throws Exception {

Example2 thread = new Example2();

System.out.println("Starting thread...");

thread.start();

Thread.sleep(3000);

System.out.println("Asking thread to stop...");

// 设置中断信号量

thread.stop = true;

Thread.sleep(3000);

System.out.println("Stopping application...");

}

public void run() {

// 每隔一秒检测一下中断信号量

while (!stop) {

System.out.println("Thread is running...");

long time = System.currentTimeMillis();

/*

* 使用while循环模拟 sleep 方法,这里不要使用sleep,否则在阻塞时会 抛

* InterruptedException异常而退出循环,这样while检测stop条件就不会执行,

* 失去了意义。

*/

while ((System.currentTimeMillis() - time < 1000)) {}

}

System.out.println("Thread exiting under request...");

}

}

使用thread.interrupt()中断非阻塞状态线程

虽然Example2该方法要求一些编码,但并不难实现。同时,它给予线程机会进行必要的清理工作。这里需注意一点的是需将共享变量定义成volatile 类型或将对它的一切访问封入同步的块/方法(synchronized blocks/methods)中。上面是中断一个非阻塞状态的线程的常见做法,但对非检测isInterrupted()条件会更简洁:

Java代码

  1. classExample2extendsThread{
  2. publicstaticvoidmain(Stringargs[])throwsException{
  3. Example2thread=newExample2();
  4. System.out.println("Startingthread...");
  5. thread.start();
  6. Thread.sleep(3000);
  7. System.out.println("Askingthreadtostop...");
  8. //发出中断请求
  9. thread.interrupt();
  10. Thread.sleep(3000);
  11. System.out.println("Stoppingapplication...");
  12. }
  13. publicvoidrun(){
  14. //每隔一秒检测是否设置了中断标示
  15. while(!Thread.currentThread().isInterrupted()){
  16. System.out.println("Threadisrunning...");
  17. longtime=System.currentTimeMillis();
  18. //使用while循环模拟sleep
  19. while((System.currentTimeMillis()-time<1000)){
  20. }
  21. }
  22. System.out.println("Threadexitingunderrequest...");
  23. }
  24. }

class Example2 extends Thread {

public static void main(String args[]) throws Exception {

Example2 thread = new Example2();

System.out.println("Starting thread...");

thread.start();

Thread.sleep(3000);

System.out.println("Asking thread to stop...");

// 发出中断请求

thread.interrupt();

Thread.sleep(3000);

System.out.println("Stopping application...");

}

public void run() {

// 每隔一秒检测是否设置了中断标示

while (!Thread.currentThread().isInterrupted()) {

System.out.println("Thread is running...");

long time = System.currentTimeMillis();

// 使用while循环模拟 sleep

while ((System.currentTimeMillis() - time < 1000) ) {

}

}

System.out.println("Thread exiting under request...");

}

}

到目前为止一切顺利!但是,当线程等待某些事件发生而被阻塞,又会发生什么?当然,如果线程被阻塞,它便不能核查共享变量,也就不能停止。这在许多情况下会发生,例如调用Object.wait()ServerSocket.accept()DatagramSocket.receive()时,这里仅举出一些。

他们都可能永久的阻塞线程。即使发生超时,在超时期满之前持续等待也是不可行和不适当的,所以,要使用某种机制使得线程更早地退出被阻塞的状态。下面就来看一下中断阻塞线程技术。

使用thread.interrupt()中断阻塞状态线程

Thread.interrupt()方法不会中断一个正在运行的线程。这一方法实际上完成的是,设置线程的中断标示位,在线程受到阻塞的地方(如调用sleepwaitjoin等地方)抛出一个异常InterruptedException,并且中断状态也将被清除,这样线程就得以退出阻塞的状态。下面是具体实现:

Java代码

  1. classExample3extendsThread{
  2. publicstaticvoidmain(Stringargs[])throwsException{
  3. Example3thread=newExample3();
  4. System.out.println("Startingthread...");
  5. thread.start();
  6. Thread.sleep(3000);
  7. System.out.println("Askingthreadtostop...");
  8. thread.interrupt();//等中断信号量设置后再调用
  9. Thread.sleep(3000);
  10. System.out.println("Stoppingapplication...");
  11. }
  12. publicvoidrun(){
  13. while(!Thread.currentThread().isInterrupted()){
  14. System.out.println("Threadrunning...");
  15. try{
  16. /*
  17. *如果线程阻塞,将不会去检查中断信号量stop变量,所thread.interrupt()
  18. *会使阻塞线程从阻塞的地方抛出异常,让阻塞线程从阻塞状态逃离出来,并
  19. *进行异常块进行相应的处理
  20. */
  21. Thread.sleep(1000);//线程阻塞,如果线程收到中断操作信号将抛出异常
  22. }catch(InterruptedExceptione){
  23. System.out.println("Threadinterrupted...");
  24. /*
  25. *如果线程在调用Object.wait()方法,或者该类的join()sleep()方法
  26. *过程中受阻,则其中断状态将被清除
  27. */
  28. System.out.println(this.isInterrupted());//false
  29. //中不中断由自己决定,如果需要真真中断线程,则需要重新设置中断位,如果
  30. //不需要,则不用调用
  31. Thread.currentThread().interrupt();
  32. }
  33. }
  34. System.out.println("Threadexitingunderrequest...");
  35. }
  36. }

class Example3 extends Thread {

public static void main(String args[]) throws Exception {

Example3 thread = new Example3();

System.out.println("Starting thread...");

thread.start();

Thread.sleep(3000);

System.out.println("Asking thread to stop...");

thread.interrupt();// 等中断信号量设置后再调用

Thread.sleep(3000);

System.out.println("Stopping application...");

}

public void run() {

while (!Thread.currentThread().isInterrupted()) {

System.out.println("Thread running...");

try {

/*

* 如果线程阻塞,将不会去检查中断信号量stop变量,所以thread.interrupt()

* 会使阻塞线程从阻塞的地方抛出异常,让阻塞线程从阻塞状态逃离出来,并

* 进行异常块进行 相应的处理

*/

Thread.sleep(1000);// 线程阻塞,如果线程收到中断操作信号将抛出异常

} catch (InterruptedException e) {

System.out.println("Thread interrupted...");

/*

* 如果线程在调用 Object.wait()方法,或者该类的 join() sleep()方法

* 过程中受阻,则其中断状态将被清除

*/

System.out.println(this.isInterrupted());// false

//中不中断由自己决定,如果需要真真中断线程,则需要重新设置中断位,如果

//不需要,则不用调用

Thread.currentThread().interrupt();

}

}

System.out.println("Thread exiting under request...");

}

}

一旦Example3中的Thread.interrupt()被调用,线程便收到一个异常,于是逃离了阻塞状态并确定应该停止。上面我们还可以使用共享信号量来替换!Thread.currentThread().isInterrupted()条件,但不如它简洁。

死锁状态线程无法被中断

Example4试着去中断处于死锁状态的两个线程,但这两个线都没有收到任何中断信号(抛出异常),所以interrupt()方法是不能中断死锁线程的,因为锁定的位置根本无法抛出异常:

Java代码

  1. classExample4extendsThread{
  2. publicstaticvoidmain(Stringargs[])throwsException{
  3. finalObjectlock1=newObject();
  4. finalObjectlock2=newObject();
  5. Threadthread1=newThread(){
  6. publicvoidrun(){
  7. deathLock(lock1,lock2);
  8. }
  9. };
  10. Threadthread2=newThread(){
  11. publicvoidrun(){
  12. //注意,这里在交换了一下位置
  13. deathLock(lock2,lock1);
  14. }
  15. };
  16. System.out.println("Startingthread...");
  17. thread1.start();
  18. thread2.start();
  19. Thread.sleep(3000);
  20. System.out.println("Interruptingthread...");
  21. thread1.interrupt();
  22. thread2.interrupt();
  23. Thread.sleep(3000);
  24. System.out.println("Stoppingapplication...");
  25. }
  26. staticvoiddeathLock(Objectlock1,Objectlock2){
  27. try{
  28. synchronized(lock1){
  29. Thread.sleep(10);//不会在这里死掉
  30. synchronized(lock2){//会锁在这里,虽然阻塞了,但不会抛异常
  31. System.out.println(Thread.currentThread());
  32. }
  33. }
  34. }catch(InterruptedExceptione){
  35. e.printStackTrace();
  36. System.exit(1);
  37. }
  38. }
  39. }

class Example4 extends Thread {

public static void main(String args[]) throws Exception {

final Object lock1 = new Object();

final Object lock2 = new Object();

Thread thread1 = new Thread() {

public void run() {

deathLock(lock1, lock2);

}

};

Thread thread2 = new Thread() {

public void run() {

// 注意,这里在交换了一下位置

deathLock(lock2, lock1);

}

};

System.out.println("Starting thread...");

thread1.start();

thread2.start();

Thread.sleep(3000);

System.out.println("Interrupting thread...");

thread1.interrupt();

thread2.interrupt();

Thread.sleep(3000);

System.out.println("Stopping application...");

}

static void deathLock(Object lock1, Object lock2) {

try {

synchronized (lock1) {

Thread.sleep(10);// 不会在这里死掉

synchronized (lock2) {// 会锁在这里,虽然阻塞了,但不会抛异常

System.out.println(Thread.currentThread());

}

}

} catch (InterruptedException e) {

e.printStackTrace();

System.exit(1);

}

}

}

中断I/O操作

然而,如果线程在I/O操作进行时被阻塞,又会如何?I/O操作可以阻塞线程一段相当长的时间,特别是牵扯到网络应用时。例如,服务器可能需要等待一个请求(request),又或者,一个网络应用程序可能要等待远端主机的响应。

实现此InterruptibleChannel接口的通道是可中断的:如果某个线程在可中断通道上因调用某个阻塞的 I/O 操作(常见的操作一般有这些:serverSocketChannel. accept()socketChannel.connectsocketChannel.opensocketChannel.readsocketChannel.writefileChannel.readfileChannel.write)而进入阻塞状态,而另一个线程又调用了该阻塞线程的 interrupt 方法,这将导致该通道被关闭,并且已阻塞线程接将会收到ClosedByInterruptException,并且设置已阻塞线程的中断状态。另外,如果已设置某个线程的中断状态并且它在通道上调用某个阻塞的 I/O 操作,则该通道将关闭并且该线程立即接收到 ClosedByInterruptException;并仍然设置其中断状态。如果情况是这样,其代码的逻辑和第三个例子中的是一样的,只是异常不同而已。

如果你正使用通道(channels)(这是在Java 1.4中引入的新的I/O API),那么被阻塞的线程将收到一个ClosedByInterruptException异常。但是,你可能正使用Java1.0之前就存在的传统的I/O,而且要求更多的工作。既然这样,Thread.interrupt()将不起作用,因为线程将不会退出被阻塞状态。Example5描述了这一行为。尽管interrupt()被调用,线程也不会退出被阻塞状态,比如ServerSocketaccept方法根本不抛出异常。

很幸运,Java平台为这种情形提供了一项解决方案,即调用阻塞该线程的套接字的close()方法。在这种情形下,如果线程被I/O操作阻塞,当调用该套接字的close方法时,该线程在调用accept地方法将接收到一个SocketExceptionSocketExceptionIOException的子异常)异常,这与使用interrupt()方法引起一个InterruptedException异常被抛出非常相似,(注,如果是流因读写阻塞后,调用流的close方法也会被阻塞,根本不能调用,更不会抛IOExcepiton,此种情况下怎样中断?我想可以转换为通道来操作流可以解决,比如文件通道)。下面是具体实现:

Java代码

  1. classExample6extendsThread{
  2. volatileServerSocketsocket;
  3. publicstaticvoidmain(Stringargs[])throwsException{
  4. Example6thread=newExample6();
  5. System.out.println("Startingthread...");
  6. thread.start();
  7. Thread.sleep(3000);
  8. System.out.println("Askingthreadtostop...");
  9. Thread.currentThread().interrupt();//再调用interrupt方法
  10. thread.socket.close();//再调用close方法
  11. try{
  12. Thread.sleep(3000);
  13. }catch(InterruptedExceptione){
  14. }
  15. System.out.println("Stoppingapplication...");
  16. }
  17. publicvoidrun(){
  18. try{
  19. socket=newServerSocket(8888);
  20. }catch(IOExceptione){
  21. System.out.println("Couldnotcreatethesocket...");
  22. return;
  23. }
  24. while(!Thread.currentThread().isInterrupted()){
  25. System.out.println("Waitingforconnection...");
  26. try{
  27. socket.accept();
  28. }catch(IOExceptione){
  29. System.out.println("accept()failedorinterrupted...");
  30. Thread.currentThread().interrupt();//重新设置中断标示位
  31. }
  32. }
  33. System.out.println("Threadexitingunderrequest...");
  34. }
  35. }class Example6 extends Thread { volatile ServerSocket socket;

public static void main(String args[]) throws Exception {

Example6 thread = new Example6();

System.out.println("Starting thread...");

thread.start();

Thread.sleep(3000);

System.out.println("Asking thread to stop...");

Thread.currentThread().interrupt();// 再调用interrupt方法

thread.socket.close();// 再调用close方法

try {

Thread.sleep(3000);

} catch (InterruptedException e) {

}

System.out.println("Stopping application...");

}

public void run() {

try {

socket = new ServerSocket(8888);

} catch (IOException e) {

System.out.println("Could not create the socket...");

return;

}

while (!Thread.currentThread().isInterrupted()) {

System.out.println("Waiting for connection...");

try {

socket.accept();

} catch (IOException e) {

System.out.println("accept() failed or interrupted...");

Thread.currentThread().interrupt();//重新设置中断标示位

}

}

System.out.println("Thread exiting under request...");

}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值