如果您需要在一台计算机上的jvm内尽可能快地安排大量(非阻塞)任务,我正在计算要使用的计时器实现。
I'm figuring what a timer implementation to use if you need to schedule tons of (non blocking) tasks as fast as possible inside jvm on one machine.
我研究过 ScheduledThreadPoolExecutor 和 HashedWheelTimer 来源(+轮定时器一般文档),这里是基本差异(N - 数字)到目前为止所有未完成的预定任务,C - 轮尺寸):
I've studied ScheduledThreadPoolExecutor and HashedWheelTimer sources (+wheel timer general docs) and here are basic differences (N - number of all outstanding scheduled tasks so far, C - wheel size):
ScheduledThreadPoolExecutor
- O(log N)用于添加新任务
- O(1)每个计时器刻度(但每个任务勾选,因此总体上为N)
- O(日志N)取消任务
- 锁定每个刻度/任务
- O(log N) for adding new task
- O(1) per each timer tick (but tick per each task, so N overall)
- O(log N) cancelling the task
- lock per each tick/task
HashedWheelTimer
- O(1)添加新任务
- 每个计时器滴答的O(m)(m~N / C,其中C> 512),所以~C整体打勾
- O(m)取消任务
- 锁定每桶任务(每次打勾)
- O(1) adding new task
- O(m) per each timer tick (m ~ N/C where C > 512 approx), so ~C ticks overall
- O(m) for cancelling a task
- lock per bucket of tasks (on each tick)
因此,我倾向于将硬件计时器用于这种用例,因为您必须以最小的开销快速安排任务,即新任务的O(1)。 此外,您将最小化簿记活动,因为您将获得更少的滴答数(N Thus I tend using HW Timer for such use-case, because you must schedule tasks quickly with minimum overhead, i.e. O(1) for new task.
Also you will minimize a bookkeeping activity, because you'll get less number of ticks (N < C) and less lock contention.
Canceling is not very important feature in this case 是否有人尝试过这些计时器进行类似的活动以及实际看到的结果? 谢谢! Did anybody try these timers for similar activities and what results see in practice?
Thanks! HWT 。除非您需要-ns-precision,否则请使用HWT。对于大多数客户端 - 服务器应用程序,HWT就足够了。在许多互联网规模的应用程序中,特别是对于超时不断变化的内存缓存,它是唯一的选择。我们在这里谈论数十亿个工作。 HWT. Unless you require to-the-ns-precision, use the HWT. For most client-server applications, a HWT is sufficient. In many internet-scale applications, especially for in-memory caches that were the timeout was constantly changing, it was the only option. We're talking about billions of jobs here. 实际上,如果你需要这种精确度,你需要一个保证中断时间而不是GC暂停的系统;即不是Java,而不是英特尔... :) Actually, if you require that level of precision, you need a system with guaranteed interrupt times and not GC pauses; i.e. not Java, not Intel... :)
更多推荐
HashedWheelTimer与ScheduledThreadPoolExecutor具有更高的性能
发布评论