Concurrency 使用ScheduledExecutorService实现非活动超时
我们的系统处理来自消息传递系统的消息。如果10秒后未收到任何消息,则应在非活动超时时引发错误 我正在考虑使用带有1个线程的ScheduledExecutorService。每次收到消息时,我都会取消上一个超时任务并提交一个新任务:Concurrency 使用ScheduledExecutorService实现非活动超时,concurrency,future,scheduledexecutorservice,Concurrency,Future,Scheduledexecutorservice,我们的系统处理来自消息传递系统的消息。如果10秒后未收到任何消息,则应在非活动超时时引发错误 我正在考虑使用带有1个线程的ScheduledExecutorService。每次收到消息时,我都会取消上一个超时任务并提交一个新任务: ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); Callable timeoutTask = new Callable() {...}; ... synchroni
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Callable timeoutTask = new Callable() {...};
...
synchronized {
timeout.cancel();
timeout = executor.schedule( timeoutTask, 10, TimeUnit.SECONDS);
}
在正常情况下,我们的处理速度约为1000/秒。这种方法可以扩展吗?如果您共享线程池,并将timeoutTask的运行时间保持在较低的水平,那么很可能就可以了。如果每1000个/秒任务有一个线程池,那么这将不起作用 如果您仍然担心,可以查看Netty项目中的HashedWheelTimer。这件事在安排超时方面非常有效。请注意,您还必须共享HashedWheelTimer的实例