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 工作而不管任何抛出和处理的异常。
调试输出:
- 维护启动 2017-08-30T14:03:05.504Z // 按预期启动并完成
- 维护完成 2017-08-30T14:03:05.566Z
- 输出:FTP连接 ID:176 220 服务就绪。
- ……
- 输出:FTP连接 ID:190 226 文件存储了 135 个字节。
- 关闭数据套接字:FTP 连接 190,/0:0:0:0:0:0:0:1:1409
- 维护启动 2017-08-30T14:03:25.581Z // 按预期启动并完成
- 维护完成 2017-08-30T14:03:25.581Z
- 读取异步异常错误。 // 出现异常
- 维护已启动 2017-08-30T14:03:45.596Z // 已启动,但未完成且再也不会 运行
- 输出:FTP连接 ID:176 221 超时,关闭控制和数据连接。
- 关闭数据套接字:FTP 连接 176,/0:0:0:0:0:0:0:1:1407
事实证明,问题出在
ACTIVECONNECTIONS.remove(connection);
我遇到了 ConcurrentModifyingException。 http://code.nomad-labs.com/2011/12/09/mother-fk-the-scheduledexecutorservice/ 中的解决方案完美运行
我有以下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 工作而不管任何抛出和处理的异常。
调试输出:
- 维护启动 2017-08-30T14:03:05.504Z // 按预期启动并完成
- 维护完成 2017-08-30T14:03:05.566Z
- 输出:FTP连接 ID:176 220 服务就绪。
- ……
- 输出:FTP连接 ID:190 226 文件存储了 135 个字节。
- 关闭数据套接字:FTP 连接 190,/0:0:0:0:0:0:0:1:1409
- 维护启动 2017-08-30T14:03:25.581Z // 按预期启动并完成
- 维护完成 2017-08-30T14:03:25.581Z
- 读取异步异常错误。 // 出现异常
- 维护已启动 2017-08-30T14:03:45.596Z // 已启动,但未完成且再也不会 运行
- 输出:FTP连接 ID:176 221 超时,关闭控制和数据连接。
- 关闭数据套接字:FTP 连接 176,/0:0:0:0:0:0:0:1:1407
事实证明,问题出在
ACTIVECONNECTIONS.remove(connection);
我遇到了 ConcurrentModifyingException。 http://code.nomad-labs.com/2011/12/09/mother-fk-the-scheduledexecutorservice/ 中的解决方案完美运行