没有从join()方法中获取到InterruptedException。

huangapple go评论65阅读模式
英文:

Didn't get the InterruptedException from the join() method

问题

InterruptedException是一个受检异常,当其他线程中断了当前线程时,会抛出此异常。但是为什么在main线程中断了在run()方法中的线程时,我没有捕获到InterruptedException呢?这里提到它会抛出InterruptedException - 如果任何线程中断了当前线程。那么为什么我在run()方法中中断线程时没有捕获到呢?

public class Test
{
    public static void main(String[] args)
    {
        try
        {
            Thread t1 = new Test1();
            t1.start();
            t1.join();
        }
        catch (InterruptedException e)
        {
            System.out.println("捕获到异常");
        }
    }
}

class Test1 extends Thread
{
    public void run()
    {
        // 确保在```main```线程中的```t1.join```执行之前执行```Thread.currentThread().interrupt();```
        for (int i = 0; i < 10_000; i++) {}

        Thread.currentThread().interrupt();
    }
}
英文:

Why I don't get InterruptedException in the main thread? Here says that it throws
InterruptedException - if any thread has interrupted the current thread. So why I don't get caught InterruptedException when I interrupted thread in the run() method?

public class Test
{
    public static void main(String[] args)
    {
        try
        {
            Thread t1 = new Test1();
            t1.start();
            t1.join();
        }
        catch (InterruptedException e)
        {
            System.out.println(&quot;exception has been caught&quot;);
        }
    }
}

class Test1 extends Thread
{
    public void run()
    {
        // to ensure that ```t1.join``` in the ```main``` thread executed earlier than ```Thread.currentThread().interrupt();```
        for (int i = 0; i &lt; 10_000; i++) {}

        Thread.currentThread().interrupt();
    }
}

答案1

得分: 2

Thread.currentThread().interrupt()

这是无用的你正在中断自己的线程没有任何意义这只会在你自己的线程上提高中断标志这意味着下一次你的线程执行任何声明抛出interruptedexception的方法时它将立即抛出异常因此毫无意义

你真正想做的是中断另一个线程。`t1.join()`是一个操作将使这个线程休眠”,直到`t1`完成这是一个可中断的操作你不是通过中断t1来实现而是通过中断你的主线程来实现

以下是一个示例

```java
public class Test
{
    public static void main(String[] args)
    {
        try
        {
            Test1 t1 = new Test1();
            t1.threadToInterrupt = Thread.currentThread();
            t1.start();
            t1.join();
        }
        catch (InterruptedException e)
        {
            System.out.println("捕获到异常");
        }
    }
}

class Test1 extends Thread
{
    Thread threadToInterrupt;
    public void run()
    {
        for (int i = 0; i < 10_000; i++) {}

        threadToInterrupt.interrupt();
    }
}
英文:

> Thread.currentThread().interrupt()

This is useless: You're interrupting your own thread, there is never any point in doing this. All this does is raise the interrupt flag on your own thread, meaning, the next time your thread executed any method that is declared to throw interruptedexception, it will immediately do so. Thus: pointless.

What you really wanted to do was interrupt the other thread. t1.join() is an operation that will 'sleep' this thread until t1 finishes. This is an interruptable operation: You do that not by interrupting t1, but by interrupting your main thread.

Here is an example:

public class Test
{
    public static void main(String[] args)
    {
        try
        {
            Test1 t1 = new Test1();
            t1.threadToInterrupt = Thread.currentThread();
            t1.start();
            t1.join();
        }
        catch (InterruptedException e)
        {
            System.out.println(&quot;exception has been caught&quot;);
        }
    }
}

class Test1 extends Thread
{
    Thread threadToInterrupt;
    public void run()
    {
        for (int i = 0; i &lt; 10_000; i++) {}

        threadToInterrupt.interrupt();
    }
}

</details>



huangapple
  • 本文由 发表于 2020年10月16日 23:39:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64392272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定