Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java线程中断标志被清除_Java_Multithreading - Fatal编程技术网

Java线程中断标志被清除

Java线程中断标志被清除,java,multithreading,Java,Multithreading,我在java中试验中断,观察到如果我中断一个线程,当线程状态从RUNNABLE变为TERMINATED interrupted变量时,不重置它的中断状态(interrupted变为false) Main.java public class Main { public static void main(String[] args) { try { t1 t1 = new t1(); t1.start(); Thread.sleep(1

我在java中试验中断,观察到如果我中断一个线程,当线程状态从RUNNABLE变为TERMINATED interrupted变量时,不重置它的中断状态(interrupted变为false)

Main.java

public class Main {

public static void main(String[] args) {

    try {

        t1 t1 = new t1();
        t1.start();

        Thread.sleep(10);
        t1.interrupt();
        System.out.println("t1.isInterrupted: " + t1.isInterrupted() + " t1.state: " + t1.getState());
        Thread.sleep(20);
        System.out.println("t1.isInterrupted: " + t1.isInterrupted() + " t1.state: " + t1.getState());

    } catch (Exception exc) {
        System.out.println("Exception: " + exc);
    }

}
public class t1 extends Thread {

@Override
public void run() {

    while (!Thread.currentThread().isInterrupted()) {
        System.out.println("Processing");
    }

    System.out.println("Execution completed - interrupt status: " + Thread.currentThread().isInterrupted());
}
}

t1.java

public class Main {

public static void main(String[] args) {

    try {

        t1 t1 = new t1();
        t1.start();

        Thread.sleep(10);
        t1.interrupt();
        System.out.println("t1.isInterrupted: " + t1.isInterrupted() + " t1.state: " + t1.getState());
        Thread.sleep(20);
        System.out.println("t1.isInterrupted: " + t1.isInterrupted() + " t1.state: " + t1.getState());

    } catch (Exception exc) {
        System.out.println("Exception: " + exc);
    }

}
public class t1 extends Thread {

@Override
public void run() {

    while (!Thread.currentThread().isInterrupted()) {
        System.out.println("Processing");
    }

    System.out.println("Execution completed - interrupt status: " + Thread.currentThread().isInterrupted());
}
}

Main.java的输出(Centos 7 64位,java 1.8.0_66-b17)

  • 处理
    处理
    处理
    t1.isInterrupted:truet1.state:RUNNABLE
    执行完成-中断状态:true
    t1.i中断:falset1.state:终止
我的问题是,是谁或是什么将此标志设置为false,以及为什么?Thread类有一个私有的exit方法,它将一些内部变量设置为null,但我找不到任何重置中断标志的方法。如果有人能解释一下这个问题,我将不胜感激

PS:这里有一个链接到

我的问题是,是谁或是什么将此标志设置为false,以及为什么

这是线程终止时的已知行为。因为它不再运行,所以中断状态为false。中断状态在线程终止之前为true,但一旦完成,则设置为false

引述:

由于线程在中断时不处于活动状态而被忽略的线程中断将通过返回false的方法反映出来


“…是谁或是什么让这面旗帜变假的?为什么?”你为什么在意?有人做了一个实现决定,所以这就是它。看看线程不再处于活动状态,因此中断状态将为false。顺便说一句,您需要学习和使用。看到类名和变量名完全相同是非常令人困惑的。变量名都应该以小写字母开头,而类名应该以大写字母开头。学习这一点并遵循这一点可以让我们更好地理解您的代码,也可以让您更好地理解其他人的代码。