ScheduledThreadPoolExecutor 停止执行并捕获异常

ScheduledThreadPoolExecutor stops executing with caught exceptions

我有以下class

public class MaintanceTools {
    public static final ScheduledThreadPoolExecutor THREADSUPERVISER;
    private static final int ALLOWEDIDLESECONDS = 1*20;

    static {
        THREADSUPERVISER = new ScheduledThreadPoolExecutor(10);
    }
    public static void launchThreadsSupervising() {
        THREADSUPERVISER.scheduleWithFixedDelay(() -> {
            System.out.println("maintance launched " + Instant.now());
            ACTIVECONNECTIONS.forEach((connection) -> {
                try {
                    if ( !connection.isDataConnected() && 
                        (connection.getLastActionInstant()
                                .until(Instant.now(), SECONDS) > ALLOWEDIDLESECONDS)) {
                    connection.closeFTPConnection();
                    ACTIVECONNECTIONS.remove(connection);
                    }
                } catch (Throwable e) { }
            });
            System.out.println("maintance finished " + Instant.now());
        }, 0, 20, TimeUnit.SECONDS);
    }
}

遍历所有 FTP 连接(因为我写 FTP 服务器),检查连接是否未传输任何数据并空闲一段时间,如果是,则关闭连接。 问题是在中断线程中抛出一些异常后,任务永远不会 运行s。我知道它写在文档中 如果任务的任何执行遇到异常,则后续执行将被抑制。否则,任务只会通过取消或终止执行者来终止。 而我有异常,但是它被捕获并且不向外抛出函数。 这个函数抛出 AsynchronousCloseException 因为它挂在 channel.read(readBuffer);当连接关闭时,抛出并捕获异常。

问题是如何使 THREADSUPERVISER 工作而不管任何抛出和处理的异常。

调试输出:

事实证明,问题出在

 ACTIVECONNECTIONS.remove(connection);

我遇到了 ConcurrentModifyingException。 http://code.nomad-labs.com/2011/12/09/mother-fk-the-scheduledexecutorservice/ 中的解决方案完美运行