我有以下课程
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服务器),检查连接是否没有传输任何数据并在一段时间内空闲,如果是这样,则关闭连接。问题是,在中断线程中抛出一些异常之后,任务永远不会运行。我知道,如果任务的任何执行遇到异常,随后的执行都会被抑制,它就会写入文档中。否则,任务将仅通过取消或终止执行器来终止。而且我也有例外,但是它是被抓到的,而且不被抛出函数。该函数抛出AsynchronousCloseException,因为它挂在channel.read(ReadBuffer)上;当连接关闭时,抛出并捕获异常。
问题是如何使THREADSUPERVISER工作,而不考虑抛出和处理异常。
调试输出:
发布于 2017-09-03 12:22:38
结果,问题出在
ACTIVECONNECTIONS.remove(connection);我有ConcurrentModifyingException。http://code.nomad-labs.com/2011/12/09/mother-fk-the-scheduledexecutorservice/中的解决方案工作得很好
https://stackoverflow.com/questions/45966488
复制相似问题