监控 ThreadPoolExecutor 抛出的运行时异常

Monitoring runtime exceptions thrown by ThreadPoolExecutor

我正在使用 ThreadPoolExecutor,其中有几个消息提供线程将消息馈送到其中进行处理。每个线程在提交之前检查执行器的深度。如果深度高于某个阈值,则线程将等待一定时间并再次检查。它只会在发现深度低于阈值时提交。所以我不认为执行者会因为消息太多而爆炸。但是,我仍然想知道我需要注意哪些例外情况。我只从 api "RejectedExecutionException" 中发现可以来自执行者:

New tasks submitted in method execute(java.lang.Runnable) will be rejected when the Executor has been shut down, and also when the Executor uses finite bounds for both maximum threads and work queue capacity, and is saturated.

RejectedExecutionException 是我需要在日志中注意的唯一异常吗?是否有人 运行 遇到过执行者陷入困境的情况?在那种情况下会发生什么?我们还会看到 RejectedExecutionException 吗?还有其他可能的例外吗?非常感谢!

我根据经验发现,与使用 Executors 相比,您需要有一个辅助函数来提供根异常 class,以便您可以正确记录它以进行诊断。 发生这种情况是因为一些执行者抛出 java.util.concurrent.TimeoutException,其他执行者抛出 java.util.concurrent.ExecutionException 取决于实现所以我想出了下面的接口

UnaryOperator<Throwable> flatEx = t -> 
    t.getCause() == null ? t : 
        t.getCause().getCause() == null ? t.getCause() :
            t.getCause().getCause().getCause() == null ? t.getCause().getCause() : 
                t.getCause().getCause().getCause();