Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.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_Interrupted Exception - Fatal编程技术网

Java 使用中断方法

Java 使用中断方法,java,multithreading,interrupted-exception,Java,Multithreading,Interrupted Exception,在步骤7(如我所标记的),主线程在线程t2上调用interrupt(),但在等待获取资源锁时,它不会抛出任何异常。之后,主线程在等待1000 ns后打印“End main”。换句话说,主线程已经完成了它的任务,那么是什么再次触发了t2.interrupt(),因为它在这之后抛出异常?下面是程序的运行方式,带有时间戳: public class TwoThreads { private static Object resource = new Object();

在步骤7(如我所标记的),主线程在线程
t2
上调用
interrupt()
,但在等待获取资源锁时,它不会抛出任何异常。之后,主线程在等待1000 ns后打印“
End main
”。换句话说,主线程已经完成了它的任务,那么是什么再次触发了
t2.interrupt()
,因为它在这之后抛出异常?

下面是程序的运行方式,带有时间戳:

public class TwoThreads {
        private static Object resource = new Object();

        private static void delay(long n) {
            try 
            { 
                Thread.sleep(n);
            }
            catch (Exception e)
            { 

                e.printStackTrace();
            }
        }

        public static void main(String[] args) {
            System.out.print("StartMain ");
            new Thread1().start();
            delay(1000);                       //dealay 1
            Thread t2 = new Thread2();
            t2.start();   
            delay(1000);                      // delay 2    
            t2.interrupt();                   //step 7
            delay(1000);                      //delay 3
            System.out.print("EndMain ");
        }

        static class Thread1 extends Thread {
            public void run() {
                synchronized (resource) {
                    System.out.print("Startl ");
                    delay(6000);
                    System.out.print("End1 ");
                }
            }
        }

        static class Thread2 extends Thread {
            public void run() {
                synchronized (resource) {
                    System.out.print("Start2 ");
                    delay(2000);
                    System.out.print("End2 ");
                }
            }
        }
    }
为什么(括号中的时间戳)

  • [0000]main启动Thread1,它获得锁并休眠6秒
  • [1000]main启动Thread2,Thread2无法获取Thread1持有的锁6秒
  • [2000]main中断Thread2,将其中断标志设置为true,但Thread2正在等待锁定,并且没有对此执行任何操作
  • [3000]主要终端
  • [6000]Thread1完成睡眠并释放锁
  • [6000]Thread2可以获取它并开始睡眠(其中断标志仍处于打开状态)
  • [6000]睡眠检测到Thread2已被中断,并立即抛出异常
  • [6000]Thread2完成,允许JVM退出

以下是程序的运行方式,带有时间戳:

public class TwoThreads {
        private static Object resource = new Object();

        private static void delay(long n) {
            try 
            { 
                Thread.sleep(n);
            }
            catch (Exception e)
            { 

                e.printStackTrace();
            }
        }

        public static void main(String[] args) {
            System.out.print("StartMain ");
            new Thread1().start();
            delay(1000);                       //dealay 1
            Thread t2 = new Thread2();
            t2.start();   
            delay(1000);                      // delay 2    
            t2.interrupt();                   //step 7
            delay(1000);                      //delay 3
            System.out.print("EndMain ");
        }

        static class Thread1 extends Thread {
            public void run() {
                synchronized (resource) {
                    System.out.print("Startl ");
                    delay(6000);
                    System.out.print("End1 ");
                }
            }
        }

        static class Thread2 extends Thread {
            public void run() {
                synchronized (resource) {
                    System.out.print("Start2 ");
                    delay(2000);
                    System.out.print("End2 ");
                }
            }
        }
    }
为什么(括号中的时间戳)

  • [0000]main启动Thread1,它获得锁并休眠6秒
  • [1000]main启动Thread2,Thread2无法获取Thread1持有的锁6秒
  • [2000]main中断Thread2,将其中断标志设置为true,但Thread2正在等待锁定,并且没有对此执行任何操作
  • [3000]主要终端
  • [6000]Thread1完成睡眠并释放锁
  • [6000]Thread2可以获取它并开始睡眠(其中断标志仍处于打开状态)
  • [6000]睡眠检测到Thread2已被中断,并立即抛出异常
  • [6000]Thread2完成,允许JVM退出

您需要一个
可重入锁定

0000 StartMain 
0000 Startl 
3000 EndMain 
6000 End1 
6000 Start2 
6000 End2 
印刷品:

public class TwoThreads {
  private static Lock lock = new ReentrantLock();

  private static void delay(long n) {
    try {
      Thread.sleep(n);
    } catch (Exception e) {

      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    System.out.print("StartMain ");
    new Thread1().start();
    delay(1000);                       //dealay 1
    Thread t2 = new Thread2();
    t2.start();
    delay(1000);                      // delay 2    
    t2.interrupt();                   //step 7
    delay(1000);                      //delay 3
    System.out.print("EndMain ");
  }

  static class Thread1 extends Thread {
    public void run() {
      try {
        lock.lockInterruptibly();
        try {
          System.out.print("Startl ");
          delay(6000);
          System.out.print("End1 ");
        } finally {
          lock.unlock();
        }
      } catch (InterruptedException ex) {
        // Interrupted.
      }
    }

  }

  static class Thread2 extends Thread {
    public void run() {
      try {
        lock.lockInterruptibly();
        try {
          System.out.print("Start2 ");
          delay(2000);
          System.out.print("End2 ");
        } finally {
          lock.unlock();
        }
      } catch (InterruptedException ex) {
        // Interrupted.
      }
    }

  }
}

您需要一个
可重入锁定

0000 StartMain 
0000 Startl 
3000 EndMain 
6000 End1 
6000 Start2 
6000 End2 
印刷品:

public class TwoThreads {
  private static Lock lock = new ReentrantLock();

  private static void delay(long n) {
    try {
      Thread.sleep(n);
    } catch (Exception e) {

      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    System.out.print("StartMain ");
    new Thread1().start();
    delay(1000);                       //dealay 1
    Thread t2 = new Thread2();
    t2.start();
    delay(1000);                      // delay 2    
    t2.interrupt();                   //step 7
    delay(1000);                      //delay 3
    System.out.print("EndMain ");
  }

  static class Thread1 extends Thread {
    public void run() {
      try {
        lock.lockInterruptibly();
        try {
          System.out.print("Startl ");
          delay(6000);
          System.out.print("End1 ");
        } finally {
          lock.unlock();
        }
      } catch (InterruptedException ex) {
        // Interrupted.
      }
    }

  }

  static class Thread2 extends Thread {
    public void run() {
      try {
        lock.lockInterruptibly();
        try {
          System.out.print("Start2 ");
          delay(2000);
          System.out.print("End2 ");
        } finally {
          lock.unlock();
        }
      } catch (InterruptedException ex) {
        // Interrupted.
      }
    }

  }
}

这是因为JVM在主线程关闭时杀死你的线程。

这是因为JVM在主线程关闭时杀死你的线程。

它们不是守护进程线程,它们不会在主线程完成时被杀死。它们不是守护进程线程,它们不会在主线程完成时被杀死。