中断被阻塞或正在休眠的线程会发生什么?

时间:2018-01-12 10:44:12

标签: java multithreading wait interrupted-exception

  

在a上调用中断方法后会发生以下哪种情况   由于先前调用连接而阻塞的线程,   睡觉或等待方法?

     
      
  1. 线程进入not-runnable状态
  2.   
  3. 线程移出not-runnable状态
  4.   
  5. 当线程进入运行状态时抛出interruptedException
  6.   
  7. 抛出InterruptedException后调用Thread.interrupted将返回true
  8.   
  9. 以上都不是。
  10.   

为回答上述问题,我创建了以下程序。

计划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);
        }
    }
}

计划1的输出:

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
State : true
runnable target
runnable target
runnable target
runnable target
runnable target
runnable target
runnable target
runnable target
runnable target
Jan 12, 2018 3:36:43 PM javaapplication1.JavaApplication1 lambda$main$1
runnable target
SEVERE: null
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at javaapplication1.JavaApplication1.lambda$main$1(JavaApplication1.java:27)
    at java.lang.Thread.run(Thread.java:748)

计划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);
        }
    }
}

计划2的输出:

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

我选择了选项1和3,因为,

  1. 状态在程序2中终止。

  2. 程序1中t2的状态为RUNNABLE,并且分别抛出睡眠中断异常。

  3. 如果t2的状态为TERMINATED,那么“runnable target”最后是如何打印4次?如果状态是TERMINATED,那么线程不应该停止运行吗?

0 个答案:

没有答案