简体   繁体   English

InterruptedException:设置中断标志时是否需要 Thread.currentThread().interrupt()

[英]InterruptedException: Is Thread.currentThread().interrupt() necessary as the interrupted flag is set

We all know that when catching an interruptedexception we are supposed to Thread.currentThread().interrupt();我们都知道,当捕获一个中断的异常时,我们应该使用 Thread.currentThread().interrupt();

However, from my tests, that flag is already set.但是,根据我的测试,该标志已经设置。

        Thread t = new Thread(() -> {
           try {
              Thread.sleep(1000L);
           } catch (InterruptedException e) {
              // Thread.currentThread().interrupt();
           }
        });
        t.start();
        System.out.println(t.isInterrupted());
        t.interrupt();
        System.out.println(t.isInterrupted());

Prints:印刷:

false
true

So what is the point of that commented out line?那么注释掉的那一行有什么意义呢?

If the line is commented out and you try to check in the thread itself if it is interrupted, you will get false as response.如果该行被注释掉并且您尝试检查线程本身是否被中断,您将得到错误作为响应。

Thread t = new Thread(() -> {
    try {
        Thread.sleep(1000L);
     } catch (InterruptedException e) {
         // Thread.currentThread().interrupt();
     }
     System.out.println(Thread.currentThread().isInterrupted());//false
 });

If you execute the line you get true.如果你执行这条线,你会得到真实的。

Thread t = new Thread(() -> {
    try {
        Thread.sleep(1000L);
     } catch (InterruptedException e) {
         Thread.currentThread().interrupt();
     }
     System.out.println(Thread.currentThread().isInterrupted());//true
 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 总是调用Thread.currentThread()。interrupt(); 捕获InterruptedException时? - Always call Thread.currentThread().interrupt(); when catching an InterruptedException? 如何使用Java Thread.currentThread()。interrupt() - How to use java Thread.currentThread().interrupt() 为什么 Thread.currentThread().interrupt() 被调用? - Why Thread.currentThread().interrupt() be called? 什么时候调用 Thread.currentThread().interrupt() 什么时候不调用? - When to call Thread.currentThread().interrupt() and when not to call? Thread.currentThread() 性能 - Thread.currentThread() performance 为什么要捕获InterruptedException来调用Thread.currentThread.interrupt()? - Why would you catch InterruptedException to call Thread.currentThread.interrupt()? 在捕获“I​​nterruptedException”之后,为什么“Thread.currentThread()。isInterrupted()”的值为false? - after catching “InterruptedException”, why “Thread.currentThread().isInterrupted()”'s value is false? Runnable#run()中的Thread.currentThread()。interrupt()与this.interrupt() - Thread.currentThread().interrupt() vs. this.interrupt() from within Runnable#run() 为什么在实现Runnable时使用Thread.currentThread()。isInterrupted()而不是Thread.interrupted()? - Why use Thread.currentThread().isInterrupted() instead of Thread.interrupted() when implementing Runnable? 在Java程序的主线程中调用System.exit(0)和Thread.currentThread()。interrupt()有什么区别? - What are the differences between calling System.exit(0) and Thread.currentThread().interrupt() in the main thread of a Java program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM