如何理解 Thread.interrupt()?

3

Java中有两种代码块。

第一种:

@Test
public void test1() {

    System.out.println("interrupt:" + Thread.currentThread().isInterrupted());
    Thread.currentThread().interrupt();
    System.out.println("interrupt:" + Thread.currentThread().isInterrupted());

}

输出:

interrupt:false
interrupt:true

第二块:

@Test
public void test2() throws InterruptedException {
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("running...");
        }
    });

    thread.interrupt();

    TimeUnit.SECONDS.sleep(2);

    System.out.println("interrupt:" + thread.isInterrupted());

    thread.start();

    TimeUnit.SECONDS.sleep(2);

    System.out.println("interrupt:" + thread.isInterrupted());

}

输出:

interrupt:false
running...
interrupt:false

因此,我的问题:

  1. 为什么在调用interrupt()后,阻塞一个interrupt:true,但不阻塞两个?
  2. 在调用interrupt()后,JVM会做什么?

谢谢!

PS:第三块代码:

@Test
public void test3() {
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("running...");
        }
    });

    thread.interrupt();

    System.out.println("interrupt:" + thread.isInterrupted());

    // thread.start();
    //
    // thread.interrupt();
    //
    //
    // System.out.println("interrupt:" + thread.isInterrupted());

}

同时输出: interrupt:false

3
在线程尚未启动之前中断它是一种无操作。 - assylias
3个回答

3
  • 在块1中,您会打断自己,这将使用中断标志“flag”线程。
  • 在块2中,您会打断其他线程(该线程尚未启动)。

在run()方法中尝试添加Thread.sleep(5000);并在中断之前启动;-)

中断的工作原理


1
编辑了你的答案,包括Java 7文档(Java 6文档没有解释在未启动的线程上调用interrupt会发生什么)。 - Perception
@Java 6文档中的Perception与Java 7相同,只是更改了CSS;-) “中断非活动线程可能没有任何效果。” - ggrandes

2

这很简单。由于sleep文档:

InterruptedException - 如果另一个线程中断了当前线程。当抛出此异常时,当前线程的中断状态将被清除。

当您调用interrupt并且线程正在睡眠时,异常被抛出,并且它仅清除了interrupted标志。

第三个块:

由于文档:中断不活动的线程可能没有任何效果。因此,总结一下:如果您的线程尚未启动,则interrupt方法可能无法正常工作。


怎么回事?为什么有人给我点了-1? - Adam Sznajder

1
java.lang.Thread的源代码中,对于interrupt方法的文档清楚地说明:

中断一个未启动的线程可能没有任何效果。

  888       /**
  889        * Interrupts this thread.
  890        *
  891        * <p> Unless the current thread is interrupting itself, which is
  892        * always permitted, the {@link #checkAccess() checkAccess} method
  893        * of this thread is invoked, which may cause a {@link
  894        * SecurityException} to be thrown.
  895        *
  896        * <p> If this thread is blocked in an invocation of the {@link
  897        * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
  898        * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
  899        * class, or of the {@link #join()}, {@link #join(long)}, {@link
  900        * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
  901        * methods of this class, then its interrupt status will be cleared and it
  902        * will receive an {@link InterruptedException}.
  903        *
  904        * <p> If this thread is blocked in an I/O operation upon an {@link
  905        * java.nio.channels.InterruptibleChannel </code>interruptible
  906        * channel<code>} then the channel will be closed, the thread's interrupt
  907        * status will be set, and the thread will receive a {@link
  908        * java.nio.channels.ClosedByInterruptException}.
  909        *
  910        * <p> If this thread is blocked in a {@link java.nio.channels.Selector}
  911        * then the thread's interrupt status will be set and it will return
  912        * immediately from the selection operation, possibly with a non-zero
  913        * value, just as if the selector's {@link
  914        * java.nio.channels.Selector#wakeup wakeup} method were invoked.
  915        *
  916        * <p> If none of the previous conditions hold then this thread's interrupt
  917        * status will be set. </p>
  918        *
  919        * <p> Interrupting a thread that is not alive need not have any effect.
  920        *
  921        * @throws  SecurityException
  922        *          if the current thread cannot modify this thread
  923        *
  924        * @revised 6.0
  925        * @spec JSR-51
  926        */
  927       public void interrupt() {
  928           if (this != Thread.currentThread())
  929               checkAccess();
  930   
  931           synchronized (blockerLock) {
  932               Interruptible b = blocker;
  933               if (b != null) {
  934                   interrupt0();           // Just to set the interrupt flag
  935                   b.interrupt(this);
  936                   return;
  937               }
  938           }
  939           interrupt0();
  940       }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接