interrupted()和isInterrupted()比较

interrupted():测试当前线程【运行此方法的当前线程】是否已经是中断状态,执行后具有将状态标志清除为false的功能。

isInterrupted():测试线程对象是否已经是中断状态,但不清除状态标志。

interrupted()的例子:

public static void main(String[] args) {
    try{
        MyThread thread = new MyThread();
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
        syso(thread.interrupted());
        syso(thread.interrupted());
    }catch(InterruptedException e) {
        e.printStackTrace();
    }
}
//运行结果
false
false

从上可以看出thread.interrupted()执行的是运行当前方法的线程,而不是thread线程,而由于main线程一直没有停止,所以一直是false。

public static void main(String[] args) {
    Thread.currentThread().interrupt();
    syso(Thread.interrupted());
    syso(Thread.interrupted());
}
//运行结果
true
false

从上可以看出Thread.currentTrhead().interrupt()将main线程停止了,所以下面第一个是true状态,而第二个是false状态,就是因为interrupted()方法有清空状态标志的功能。

isInterrupted()的例子:

public static void main(String[] args) {
    try{
        MyThread thread = new MyThread();
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
        syso(thread.isInterrupted());
        syso(thread.isInterrupted());
    }catch(InterruptedException e) {
        e.printStackTrace();
    }
}
//运行结果
true
true

从上面可以看出isInterrupted()就是线程对象是否中断。

interrupt()并不能真正的结束线程:

public class MyThread extends Thread{
    public void run() {
        super.run();
        for(int i = 0; i < 5; i++) {
            if(this.interrupted()) {
                System.out.println("已经是停止状态了!我要退出了!");
                break;
            }
            System.out.println("i = " + i);
        }
        System.out.println("for之后代码");
    }
}
public class Main {

    public static void main(String[] args) {
        try {
            MyThread thread = new MyThread();
            thread.start();
            Thread.sleep(2000);
            thread.interrupt();
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束了");
    }

}

运行结果:

可以发现interrupt()并没有真正终止线程。下面介绍几种可以终止线程的方法:

1.异常法【建议使用】:

if(this.interrupted()) {
                System.out.println("已经是停止状态了!我要退出了!");
    throw new InterruptedException();
                break;
            }

在上面需要正常终止的地方抛出异常即可。

此方法最推荐使用,因为在catch块中还可以将异常向上抛,使线程停止的事件得以传播。

2.在sleep中interrupt():

public class SleepThread extends Thread{
    public void run() {
        super.run();
        try {
            System.out.println("run begin");
            Thread.sleep(200000);
            System.out.println("run end");
        } catch(InterruptedException e) {
            System.out.println("沉睡中停止,进入catch");
            e.printStackTrace();
        }
    }
}
public class SleepMain {

    public static void main(String[] args) {
        try {
            SleepThread thread = new SleepThread();
            thread.start();
            Thread.sleep(200);
            thread.interrupt();
        } catch(InterruptedException e) {
            System.out.println("main catch");
            e.printStackTrace();
        }
        System.out.println("结束了");
    }

}

运行结果:

3.使用作废的stop()方法:

调用stop()方法会强制让线程停止则有可能使一些清理性的工作得不到完成。并且有可能对锁定对象进行了“解锁”,导致数据得不到同步处理,出现数据不一致问题。

4.return法:

public class ReturnThread extends Thread{
    public void run() {
        while(true) {
            if(this.isInterrupted()) {
                System.out.println("停止了!");
                return;
            }
            System.out.println("timer" + System.currentTimeMillis());
        }
    }
}
public class ReturnMain {

    public static void main(String[] args) throws InterruptedException {
        ReturnThread thread = new ReturnThread();
        thread.start();
        Thread.sleep(20);
        thread.interrupt();
    }

}

在需要停止的地方return即可。

原文地址:https://www.cnblogs.com/cing/p/8997582.html

时间: 2024-08-03 12:09:56

interrupted()和isInterrupted()比较的相关文章

java---interrupt、interrupted和isInterrupted的区别

1.interrupt interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位,不会停止线程.需要用户自己去监视线程的状态为并做处理.支持线程中断的方法(也就是线程中断后会抛出interruptedException的方法)就是在监视线程的中断状态,一旦线程的中断状态被置为"中断状态",就会抛出中断异常. 2.interrupted 和 isInterrupted 首先看一下该方法的实现: 1 pu

interrupt ,interrupted 和 isInterrupted

1.interrupt interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位,不会停止线程.需要用户自己去监视线程的状态为并做处理.支持线程中断的方法(也就是线程中断后会抛出interruptedException的方法)就是在监视线程的中断状态,一旦线程的中断状态被置为"中断状态",就会抛出中断异常. 2.interrupted 和 isInterrupted 首先看一下该方法的实现: 1 pu

JAVA多线程之中断机制(stop()、interrupted()、isInterrupted())

一,介绍 本文记录JAVA多线程中的中断机制的一些知识点.主要是stop方法.interrupted()与isInterrupted()方法的区别,并从源代码的实现上进行简单分析. JAVA中有3种方式可以终止正在运行的线程 ①线程正常退出,即run()方法执行完毕了 ②使用Thread类中的stop()方法强行终止线程.但stop()方法已经过期了,不推荐使用 ③使用中断机制 线程正常退出没有什么东东,中断机制下面详细介绍,先看下stop()方法的源代码,关键是源代码上的注释.它解释了为什么s

interrupt、interrupted和isInterrupted的区别

1.interrupt() interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位,不会停止线程.需要用户自己去监视线程的状态为并做处理. 2.interrupted() 和 isInterrupted() public static boolean interrupted () { return currentThread().isInterrupted(true); } public boolean isInter

interrupt、interrupted 、isInterrupted 区别

       线程中断是一种协作机制,线程可以通过这种机制来通知另一个线程,告诉他在合适的或者可能的情况下停止当前工作,并转而执行其他的工作.       通过中断并不能直接终止另一个线程,而需要被中断的线程自己处理中断. 这好比是家里的父母叮嘱在外的子女要注意身体,但子女是否注意身体,怎么注意身体则完全取决于自己. ‍‍‍‍‍‍‍每一个线程都有一个boolean类型的中断状态.当中断线程时,这个线程的中断状态将被设置为true.‍‍‍‍‍‍‍在Thread中包含了中断线程以及查询线程中断状态的

java 多线程5: java 终止线程及中断机制 (stop()、interrupt() 、interrupted()、isInterrupted())

JAVA中有3种方式可以终止正在运行的线程 ①线程正常退出,即run()方法执行完毕了 ②使用Thread类中的stop()方法强行终止线程.但stop()方法已经过期了,不推荐使用 ③使用中断机制interrupt() 1.stop()方法 stop()在java多线程中已经废弃 1.stop()方法会导致释放锁的不良后果,数据不完整 比如一个上锁了得方法: threadA线程拥有了监视器,这些监视器负责保护某些临界资源,比如说银行的转账的金额.当正在转账过程中,main线程调用 thread

Thread中interrupt()interrupted()和isInterrupted()的区别

在java线程中,线程的状态分为6种.官方文档的解释是:         /**          * Thread state for a thread which has not yet started.          */         NEW,         /**          * Thread state for a runnable thread.  A thread in the runnable          * state is executing in th

java interrupted与isInterrupted方法

interrupted:测试当前线程是否是中断状态,执行完清除中断状态 isInterrupted:测试Thread对象是否是中断状态,不清除中断状态 public static boolean interrupted() {        return currentThread().isInterrupted(true);    } /**     * Tests whether this thread has been interrupted.  The <i>interrupted  

interrupt interrupted isInterrupted 区别

1.interrupt interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位,不会停止线程.需要用户自己去监视线程的状态为并做处理.支持线程中断的方法(也就是线程中断后会抛出interruptedException的方法)就是在监视线程的中断状态,一旦线程的中断状态被置为"中断状态",就会抛出中断异常. 2.interrupted 和 isInterrupted 首先看一下该方法的实现: 1 pu