Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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,我只是在研究线程和中断。我有一个小的工作例子。不幸的是我没有 请参阅ineterrupt正在工作。有人能告诉我代码有什么问题吗:- public class SleeperTest{ public static void main(String[] args) throws InterruptedException{ Runnable sleeper = new Runnable(){ public void run(){

我只是在研究线程和中断。我有一个小的工作例子。不幸的是我没有 请参阅ineterrupt正在工作。有人能告诉我代码有什么问题吗:-

public class SleeperTest{
    public static void main(String[] args) throws InterruptedException{
        Runnable sleeper = new Runnable(){
            public void run(){
                // try{
                // System.out.println("Sleeper is going to sleep");
                // Thread.sleep(2000);
                // System.out.println("Sleeper is awake");
                // }
                // catch(InterruptedException e){
                // System.out.println("Sleeper got interrupted");
                // }
                // System.out.println("Hello Ben");
                if(Thread.interrupted()){
                System.out.println("Thread has been interrupted");
                System.out.println(Thread.currentThread().isInterrupted());
                }

            }
        };

        Thread t = new Thread(sleeper);
        t.start();
        Thread.sleep(500);
        t.interrupt();
    }
}
它不会去被打断的街区。有人能告诉我为什么吗

谢谢,

你有比赛条件。创建的线程在主线程可以中断它之前执行并通过if块

中断通常用于停止或通知在循环中运行的长时间运行的线程。试试像这样的东西

while (!Thread.interrupted()) {
    System.out.println("not interrupted");
}
System.out.println("interrupted");
// then exits

我将尝试在数量上为这两个线程注释一个可能的执行流

主线程:

枕木螺纹:


这几乎可以肯定是一种比赛条件。您的休眠线程可能会立即检查它是否被中断,然后退出,特别是因为您的第一个线程正在休眠半秒,这比另一个线程调度和执行所需的时间要长得多。当主线程开始执行t.interrupt时,您的睡眠线程已完成执行@m3th0dman仍然可能获得竞争条件,因为竞争将取决于中断是否会在if之前发生statement@m3th0dman这不会消除竞争条件。然后,他可能会看到两种不同的行为,具体取决于线程执行的时间。基本上,您生成的线程会与原始线程进行竞争,前者试图检查是否已被中断。非常感谢您的解释。我明白你的意思,完全理解你的解释。我无法理解本例中的争用条件。@benz我想在本例中,争用是在尝试查看其是否已被中断的繁殖线程和尝试在睡眠后中断繁殖线程的原始线程之间进行的。在这种情况下,生成的线程总是因为睡眠而获胜,但从技术上讲,这仍然是一种竞争条件。@user3580294非常感谢。我完全明白你的解释。非常感谢你。
        Thread t = new Thread(sleeper);
        t.start();                           <<< (1) your thread is started; its execution now runs in parallel to this thread
        Thread.sleep(500);                   <<< (2) this thread is now sleeping; the "sleeper" thread is still running!
        t.interrupt();                       <<< (3) 500 milliseconds (0.5 second) later, trying to interrupt, but that thread is most likely already finished by now; this is a no-op
        public void run() {                   <<< (1) this thread starting 
            if (Thread.interrupted()) {       <<< (2.1) Am I interrupted? (Not yet, so no)
                 System.out.println("Thread has been interrupted");
                 System.out.println(Thread.currentThread().isInterrupted());
                                              <<< (2.2) Finished executing. This thread is done.
            }
        }