Java教程

Java 中断线程

如果任何线程处于睡眠或等待状态(即调用sleep()或wait()),则在该线程上调用interrupt()方法将抛出睡眠状态或等待状态,并抛出InterruptedException。如果线程未处于睡眠或等待状态,则调用interrupt()方法将执行正常行为,并且不会中断线程,而是将中断标志设置为true。首先,让我们看看Thread类提供的用于中断线程的方法。

Thread类提供的3种中断线程的方法

public void interrupt() public static boolean interrupted() public boolean isInterrupted()

中断正在停止工作的线程的示例

在此示例中,在中断线程之后,我们正在传播该线程,因此它将停止工作。如果我们不想停止线程,则可以在调用sleep()或wait()方法的地方进行处理。首先让我们看一下传播异常的示例。
class TestInterruptingThread1 extends Thread{
    public void run(){
        try{
            Thread.sleep(1000);
            System.out.println("task");
        }
        catch(InterruptedException e){
            throw new RuntimeException("Thread interrupted..."+e);
        }
    }
    public static void main(String args[]){
        TestInterruptingThread1 t1=new TestInterruptingThread1();
        t1.start();
        try{
            t1.interrupt();
        }
        catch(Exception e){
            System.out.println("Exception handled "+e);
        }
    }
}
Output:Exception in thread-0 java.lang.RuntimeException: Thread interrupted... java.lang.InterruptedException: sleep interrupted at A.run(A.java:7)

中断无法停止工作的线程的示例

在此示例中,在中断线程之后,我们将处理异常,因此它将打破休眠状态,但不会停止工作。
class TestInterruptingThread2 extends Thread{
    public void run(){
        try{
            Thread.sleep(1000);
            System.out.println("task");
        }
        catch(InterruptedException e){
            System.out.println("Exception handled "+e);
        }
        System.out.println("thread is running...");
    }
    public static void main(String args[]){
        TestInterruptingThread2 t1=new TestInterruptingThread2();
        t1.start();
        t1.interrupt();
    }
}
Output:Exception handled java.lang.InterruptedException: sleep interrupted thread is running...

中断正常运行的线程的示例

如果线程未处于睡眠或等待状态,则调用interrupt()方法会将interrupted标志设置为true,该标志可稍后由Java程序员用来停止线程。
class TestInterruptingThread3 extends Thread{
    public void run(){
        for(int i=1;i<=5;i++)System.out.println(i);
    }
    public static void main(String args[]){
        TestInterruptingThread3 t1=new TestInterruptingThread3();
        t1.start();
        t1.interrupt();
    }
}
Output:1 2 3 4 5

isInterrupted和interrupted方法怎么样?

isInterrupted()方法返回中断标志true或false。静态interrupted()方法返回中断标志,然后将其设置为false(如果为true)。
public class TestInterruptingThread4 extends Thread{
    public void run(){
        for(int i=1;i<=2;i++){
            if(Thread.interrupted()){
                System.out.println("code for interrupted thread");
            }
            else{
                System.out.println("code for normal thread");
            }
        }
    //end of for loop}
    public static void main(String args[]){
        TestInterruptingThread4 t1=new TestInterruptingThread4();
        TestInterruptingThread4 t2=new TestInterruptingThread4();
        t1.start();
        t1.interrupt();
        t2.start();
    }
}
Output:Code for interrupted thread code for normal thread code for normal thread code for normal thread
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4