加入线程时处理异常的最佳方式 [英] Best way to handle exception when joining a thread

查看:110
本文介绍了加入线程时处理异常的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我对以下内容感到困惑:

假设我有主题A ,绝对需要执行线程B 已完成处理。

一种方法是通过线程A 加入线程B

简单示例:

For some reason I am confused over the following:
Assume that I have Thread A that absolutely needs to execute after Thread B has completed its processing.
A way to do this would be by Thread A joining Thread B.
Trivial example:

public class MainThread {  
    public static void main(String[] args){   
        Thread b = new Thread (new SomeRunnable(args[0]));  
        b.start();  
        try {   
            b.join();   
        } catch(InteruptedException e) {  
        }  
        // Go on with processing  
    }
}

我的问题如下:在这种情况下处理异常的正确方法是什么?

My question is the following: What is the proper way to handle the exception in such a case?

在我看到的各种例子中,即使在文本书中,异常也被忽略。

所以如果线程A 需要确保线程B 在程序完成之前,如果我在一个异常终止在catch,可以是线程B 可能仍然可以运行/运行?那么处理这个异常的最好方法是什么?

In various example I have seen, even in text-books, the exception is ignored.
So if Thread A needs to be sure that Thread B is completely finished before proceding, if I end up in the catch due to an exception, can it be the case that Thread B may still actually be runnable/running? So what is the best way to handle this exception?

推荐答案


在这种情况下的异常?

What is the proper way to handle the exception in such a case?

任何时候你得到一个 InterruptedException 当前线程应该考虑自己被中断。通常,这意味着线程应该在其自身之后清理并退出。在你的情况下,主线程正被另一个线程中断,并应该中断 Thread a ,它依次启动,然后退出。

Any time you get an InterruptedException the current thread should consider itself to be interrupted. Typically, that means that the thread should clean up after itself and exit. In your case, the main thread is being interrupted by another thread and should probably interrupt the Thread a that it started in turn, and then quit.

虽然这取决于你是否应该忽略中断,我建议这是一个坏的做法。如果你使用中断作为线程的某种信号,我会改为设置一些 volatile boolean 标志。

Although it is up to you whether the interrupt should be ignored I would suggest that it is a bad practice. If you were using the interrupt as some sort of signal to the thread then I would instead set some volatile boolean flag.

根据捕获 InterruptedException 时的最佳实践,通常我执行:

In terms of best practice when catching InterruptedException, typically I do:

try {
    ...
} catch(InterruptedException e){  
    // a good practice to re-enable the interrupt flag on the thread
    Thread.currentThread().interrupt();
    // in your case you probably should interrupt the Thread a in turn
    a.interrupt();
    // quit the thread
    return;
}

由于捕获 InterruptedException 清除线程的中断标志,在catch块中重新启用中断标志始终是个好主意。

Since catching the InterruptedException clears the interrupt flag for the thread, it is always a good idea to re-enable the interrupt flag in the catch block.


我已经看到的各种例子,即使在教科书中,例外被忽略。

In various example I have seen, even in text-books, the exception is ignored.

确实。忽略任何异常是非常糟糕的做法,但它会一直发生。不要放弃黑暗的力量!

Indeed. It is very bad practice to ignore any exception but it happens all of the time. Don't give into the dark forces!


可能是线程B实际上仍然可以运行/运行的情况?

can it be the case that Thread B may still actually be runnable/running?

线程B 当然可以运行。它是调用已被中断的 join()的主线程。

Thread B can certainly still be running. It is the main thread that is calling the join() that has been interrupted.

这篇关于加入线程时处理异常的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆