java - 如何为线程组设置UncaughtExceptionHandler

标签 java multithreading threadgroup

假设我有 5 个线程,属于名为“Fruits-group”的线程组。

如何一次性将 UncaughtExceptionHandler 分配给 Fruits-group 的所有线程?

我知道我们可以为所有线程定义一个全局UncaughtExceptionHandler。 我正在寻找的是将 UncaughtExceptionHandler 分配给整个线程组?

最佳答案

TL;DR子类化ThreadGroup并重写uncaughtException()方法。

一个ThreadGroup UncaughtExceptionHandler ,实现uncaughtException(Thread t, Throwable e)方法:

Called by the Java Virtual Machine when a thread in this thread group stops because of an uncaught exception, and the thread does not have a specific Thread.UncaughtExceptionHandler installed.

The uncaughtException method of ThreadGroup does the following:

  • If this thread group has a parent thread group, the uncaughtException method of that parent is called with the same two arguments.
  • Otherwise, this method checks to see if there is a default uncaught exception handler installed, and if so, its uncaughtException method is called with the same two arguments.
  • Otherwise, this method determines if the Throwable argument is an instance of ThreadDeath. If so, nothing special is done. Otherwise, a message containing the thread's name, as returned from the thread's getName method, and a stack backtrace, using the Throwable's printStackTrace method, is printed to the standard error stream.

Applications can override this method in subclasses of ThreadGroup to provide alternative handling of uncaught exceptions.

<小时/>

更新

如果您希望能够为ThreadGroup设置一个UncaughtExceptionHandler,您可以创建一个委托(delegate)子类:

public class ExceptionHandlingThreadGroup extends ThreadGroup {
    private UncaughtExceptionHandler uncaughtExceptionHandler;
    
    public ExceptionHandlingThreadGroup(String name) {
        super(name);
    }
    public ExceptionHandlingThreadGroup(ThreadGroup parent, String name) {
        super(parent, name);
    }
    
    public UncaughtExceptionHandler getUncaughtExceptionHandler() {
        return this.uncaughtExceptionHandler;
    }
    public void setUncaughtExceptionHandler(UncaughtExceptionHandler uncaughtExceptionHandler) {
        this.uncaughtExceptionHandler = uncaughtExceptionHandler;
    }
    
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        if (this.uncaughtExceptionHandler != null)
            this.uncaughtExceptionHandler.uncaughtException(t, e);
        else
            super.uncaughtException(t, e);
    }
}

不过,一般来说,直接在子类中实现异常处理逻辑可能会更好,但这样,您可以使用现有的UncaughtExceptionHandler实现.

关于java - 如何为线程组设置UncaughtExceptionHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46226650/

相关文章:

java - 如何使用字符串或整数以外的数据类型启动 Intent?

java - JSpinner 和双点或逗号作为分隔符

c++ - 处理多线程清理的最佳方式

java - 我可以检索在 ScheduledExecutorService 中运行的任务而不保留其引用吗?

python - 为什么从工作线程更新 tkinter 小部件似乎有效?

java - 如何在 Java 中阻塞一组线程

multithreading - java.net.SocketException : Socket closed at the end of the script duration when using Jmeter Concurrency Thread Group

java - IF EXISTS 语句的语法 SQL 错误

java - 从 xml 文档中提取纯文本的最简单方法是什么?

java - 加入一组总超时的线程