首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何理解Thread.interrupt()?

我如何理解Thread.interrupt()?
EN

Stack Overflow用户
提问于 2013-01-06 13:27:11
回答 3查看 647关注 0票数 3

java中有两个代码块。

第一区:

代码语言:javascript
复制
@Test
public void test1() {

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

}

输出:

代码语言:javascript
复制
interrupt:false
interrupt:true

第二部分:

代码语言:javascript
复制
@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());

}

输出:

代码语言:javascript
复制
interrupt:false
running...
interrupt:false

所以,我的问题是:

  1. 为什么在调用interrupt:true之后阻止一个打印interrupt(),而不阻止第二个呢?
  2. JVM在调用中断()之后将做什么?

谢谢!

PS:第三项:

代码语言:javascript
复制
@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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-06 13:38:48

  • 在第1栏中,您中断了自己的操作,这个“标志”是带有中断标志的线程。
  • 在第2块中,中断其他线程(即没有活动的-not已启动-)

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

中断是如何工作的

票数 3
EN

Stack Overflow用户

发布于 2013-01-06 13:31:07

这相当简单。由于sleep文档:

InterruptedException - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

当您调用interrupt并且线程处于休眠状态时,异常将被抛出,它只是清除了interrupted标志。

第三项:

由于文档:Interrupting a thread that is not alive need not have any effect.,所以总结一下:如果您的线程尚未启动,interrupt方法可能无法工作。

票数 2
EN

Stack Overflow用户

发布于 2013-01-06 13:55:48

java.lang.Thread的源代码中,对于interrupt方法,文档明确指出:

中断没有活动的线程不需要有任何效果。

代码语言:javascript
复制
  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       }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14182585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档