Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Concurrency 使用ScheduledExecutorService实现非活动超时_Concurrency_Future_Scheduledexecutorservice - Fatal编程技术网

Concurrency 使用ScheduledExecutorService实现非活动超时

Concurrency 使用ScheduledExecutorService实现非活动超时,concurrency,future,scheduledexecutorservice,Concurrency,Future,Scheduledexecutorservice,我们的系统处理来自消息传递系统的消息。如果10秒后未收到任何消息,则应在非活动超时时引发错误 我正在考虑使用带有1个线程的ScheduledExecutorService。每次收到消息时,我都会取消上一个超时任务并提交一个新任务: ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); Callable timeoutTask = new Callable() {...}; ... synchroni

我们的系统处理来自消息传递系统的消息。如果10秒后未收到任何消息,则应在非活动超时时引发错误

我正在考虑使用带有1个线程的ScheduledExecutorService。每次收到消息时,我都会取消上一个超时任务并提交一个新任务:

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的实例