Java 当中断被阻塞或休眠的线程时会发生什么?

Java 当中断被阻塞或休眠的线程时会发生什么?,java,multithreading,wait,interrupted-exception,Java,Multithreading,Wait,Interrupted Exception,以下哪一种情况发生在调用 由于先前调用联接而阻塞的线程, 睡眠还是等待 线程将进入不可运行状态 线程将移出不可运行状态 当线程进入运行状态时,会引发interruptedException 抛出InterruptedException后调用Thread.interrupted将返回true 以上都没有 为了回答上述问题,我创建了以下程序 计划1 public class JavaApplication1 { public static void main(String[] args)

以下哪一种情况发生在调用 由于先前调用联接而阻塞的线程, 睡眠还是等待

  • 线程将进入不可运行状态
  • 线程将移出不可运行状态
  • 当线程进入运行状态时,会引发interruptedException
  • 抛出InterruptedException后调用Thread.interrupted将返回true
  • 以上都没有
  • 为了回答上述问题,我创建了以下程序

    计划1

    public class JavaApplication1 {
    
        public static void main(String[] args) {
            try {
                Thread.sleep(2000);
    
                boolean interrupted = Thread.interrupted();
                System.out.println(interrupted);
                Thread currentThread = Thread.currentThread();
                System.out.println("Current Thread ID : " + currentThread.getId());
                System.out.println("State : " + currentThread.getState());
                Thread t = new Thread(() -> System.out.println("runnable target"));
                System.out.println("Thread t not started yet : " + t.getState());
                System.out.println("Thread t not started yet : " + t.isAlive());
                System.out.println("T id : " + t.getId());
    
                Thread t2 = new Thread(() -> {
                    for (int i = 0; i < 10; i++) {
                        try {
                            System.out.println("runnable target");
                            if (i == 5) {
                                Thread.sleep(2000);
                            }
                        } catch (InterruptedException ex) {
                            Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
    
                });
                t2.start();
                System.out.println("T id : " + t2.getId());
                System.out.println("default priority : " + t2.getPriority());
    
                System.out.println("interrupting the main thread---");
                currentThread.interrupt();
                System.out.println("Current Thread ID : " + currentThread.getId());
                System.out.println("State : " + currentThread.getState());
                System.out.println("State : " + currentThread.isInterrupted());
    
                System.out.println("interrupting the t2 thread---");
                t2.interrupt();
                System.out.println("Current Thread ID : " + t2.getId());
                System.out.println("State : " + t2.getState());
                System.out.println("State : " + t2.isInterrupted());
    
            } catch (InterruptedException ex) {
                System.out.println("Interrupted Exception :" + ex);
            }
        }
    }
    
    计划2

    public class JavaApplication1 {
    
        public static void main(String[] args) {
            try {
                Thread.sleep(2000);
    
                boolean interrupted = Thread.interrupted();
                System.out.println(interrupted);
                Thread currentThread = Thread.currentThread();
                System.out.println("Current Thread ID : " + currentThread.getId());
                System.out.println("State : " + currentThread.getState());
                Thread t = new Thread(() -> System.out.println("runnable target"));
                System.out.println("Thread t not started yet : " + t.getState());
                System.out.println("Thread t not started yet : " + t.isAlive());
                System.out.println("T id : " + t.getId());
    
                Thread t2 = new Thread(() -> {
                    for (int i = 0; i < 10; i++) {
                        try {
                            System.out.println("runnable target");
                            if (i == 5) {
                                System.out.println("t2 interrupted? "+Thread.interrupted());
                                System.out.println("State : "+currentThread.getState());
                                Thread.sleep(2000);
                            }
                        } catch (InterruptedException ex) {
                            Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
    
                });
                t2.start();
                System.out.println("T id : " + t2.getId());
                System.out.println("default priority : " + t2.getPriority());
    
                System.out.println("interrupting the main thread---");
                currentThread.interrupt();
                System.out.println("Current Thread ID : " + currentThread.getId());
                System.out.println("State : " + currentThread.getState());
                System.out.println("is interrupted : " + currentThread.isInterrupted());
    
                System.out.println("interrupting the t2 thread---");
                t2.interrupt();
                System.out.println("Current Thread ID : " + t2.getId());
                System.out.println("State : " + t2.getState());
                System.out.println("is interrupted : " + t2.isInterrupted());
    
            } catch (InterruptedException ex) {
                System.out.println("Interrupted Exception :" + ex);
            }
        }
    }
    
    我选择选项1和3是因为

  • 该状态在程序2中终止

  • t2的状态在程序1中是可运行的,并且分别引发睡眠中断异常


  • 如果t2的状态终止,如何在结束时将“可运行目标”打印4次?如果线程的状态是终止的,它不应该停止运行吗?

    我认为,因为线程“t”不会抛出InterruptedException。您在这里运行了三个线程,其中只有两个(我相信)调用了抛出InteruptedException所需的方法。From-'如果此线程在调用对象类的wait()、wait(long)或wait(long,int)方法时被阻塞,或者在调用此类的join()、join(long)、join(long,int)、sleep(long)或sleep(long,int)方法时被阻塞,然后它的中断状态将被清除,它将收到InterruptedException。“@CowboyFarnz感谢您的评论,但只有两个线程正在运行。主线程和t2。。pl注意,我没有调用thread对象的start方法t@CowboyFarnz是的,我在发布问题后参考了API。它没有说任何关于线程所处状态的内容,它抛出了一个InterruptedException,它清除了线程的中断状态(“然后它的中断状态将被清除”-因此重置它),但线程将继续运行,这是预期的行为,因为它的设计方式(我同意这是一个缺陷)。尝试跳出for循环并测试其行为。当该行的print语句只打印一个布尔值时,您的代码如何打印“被中断:终止”?不管怎样,我运行了第二个代码段,即使在多次运行之后,也无法使其显示为任何状态的终止。
    public class JavaApplication1 {
    
        public static void main(String[] args) {
            try {
                Thread.sleep(2000);
    
                boolean interrupted = Thread.interrupted();
                System.out.println(interrupted);
                Thread currentThread = Thread.currentThread();
                System.out.println("Current Thread ID : " + currentThread.getId());
                System.out.println("State : " + currentThread.getState());
                Thread t = new Thread(() -> System.out.println("runnable target"));
                System.out.println("Thread t not started yet : " + t.getState());
                System.out.println("Thread t not started yet : " + t.isAlive());
                System.out.println("T id : " + t.getId());
    
                Thread t2 = new Thread(() -> {
                    for (int i = 0; i < 10; i++) {
                        try {
                            System.out.println("runnable target");
                            if (i == 5) {
                                System.out.println("t2 interrupted? "+Thread.interrupted());
                                System.out.println("State : "+currentThread.getState());
                                Thread.sleep(2000);
                            }
                        } catch (InterruptedException ex) {
                            Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
    
                });
                t2.start();
                System.out.println("T id : " + t2.getId());
                System.out.println("default priority : " + t2.getPriority());
    
                System.out.println("interrupting the main thread---");
                currentThread.interrupt();
                System.out.println("Current Thread ID : " + currentThread.getId());
                System.out.println("State : " + currentThread.getState());
                System.out.println("is interrupted : " + currentThread.isInterrupted());
    
                System.out.println("interrupting the t2 thread---");
                t2.interrupt();
                System.out.println("Current Thread ID : " + t2.getId());
                System.out.println("State : " + t2.getState());
                System.out.println("is interrupted : " + t2.isInterrupted());
    
            } catch (InterruptedException ex) {
                System.out.println("Interrupted Exception :" + ex);
            }
        }
    }
    
    false
    Current Thread ID : 1
    State : RUNNABLE
    Thread t not started yet : NEW
    Thread t not started yet : false
    T id : 10
    T id : 11
    default priority : 5
    interrupting the main thread---
    Current Thread ID : 1
    State : RUNNABLE
    State : true
    interrupting the t2 thread---
    Current Thread ID : 11
    State : RUNNABLE
    is interrupted : true
    runnable target
    runnable target
    runnable target
    runnable target
    runnable target
    runnable target
    t2 interrupted? true
    is interrupted : TERMINATED
    runnable target
    runnable target
    runnable target
    runnable target