当线程被杀死时,是否有任何地方可以连接?就像是 :

onDestroy{
 //do something...
}

编辑:
对不起。我应该更清楚地说明它。

线程终止不是因为所有工作都已经完成,而是因为它已被使用ThreadGroup.destroy()的客户端代码杀死。由于我的单例是在客户端代码中实例化的,因此它将属于客户端代码的 THreadGroup 并因此被杀死。 (其实最后一句我也不是很确定……)

最佳答案

您可以像这样包装 action 和 hook。

public final class HookOnDestroy implements Runnable {
    private final Runnable action;
    private final Runnable hook;
    public HookOnDestroy(Runnable action, Runnable hook) {
          this.hook = hook;
          this.action = action;
    }

    @Override
    public void run() {
       try {
         action.run();
       } finally {
         hook.run();
       }
    }

}


Runnable action = ...
Runnable hook = ...
new Thread( new HookOnDestroy(action,hook)).start();

10-07 19:40
查看更多