英文:
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("exception has been caught");
}
}
}
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 < 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("exception has been caught");
}
}
}
class Test1 extends Thread
{
Thread threadToInterrupt;
public void run()
{
for (int i = 0; i < 10_000; i++) {}
threadToInterrupt.interrupt();
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论