简体   繁体   English

在java预定执行程序服务中处理sleep

[英]handling sleep in java scheduled executor service

I have a sort of complex problem like below. 我有一个像下面这样复杂的问题。 - we have a real time system with large number threads requirement. - 我们有一个具有大量线程要求的实时系统。 In order to optimize the performance, we are thinking of following design. 为了优化性能,我们正在考虑以下设计。

  • create a thread pool executor with max number of threads 创建一个具有最大线程数的线程池执行程序
  • each thread is used to create scheduled executor service. 每个线程用于创建预定的执行程序服务。
  • now the tasks are being assigned to these executor services evenly based on load 现在,根据负载均匀地将任务分配给这些执行器服务

  • BUT the biggest problem is, if one of the task in the queue contains a sleep (for few secs), it blocks the corresponding Schedule executor service thread for that duration and subsequently all the following tasks in that queue. 但最大的问题是,如果队列中的任务之一包含休眠(几秒),它会在该持续时间内阻塞相应的Schedule executor服务线程,并随后阻塞该队列中的所有后续任务。

  • In this regard, please suggest me how to suspend the execution of the task with sleep OR overriding the sleep somehow and rejoin/schedule the task again to the queue. 在这方面,请建议我如何暂停执行睡眠或以某种方式覆盖睡眠,并重新加入/重新安排任务到队列。

Thanks in advance Seshu 在此先感谢Seshu

Assuming I understand your question, your Schedule Executor service threads have a deadline requirement, but the actual workers can sleep for an unknown length of time, possibly throwing off the timing of the Schedule Executors. 假设我理解你的问题,你的Schedule Executor服务线程有一个截止日期要求,但实际的工作人员可以睡一段未知的时间,可能会导致Schedule Executors的时间丢失。 From your description I'm guessing what you want is for a task that needs to sleep to actually stop, save progress information and then requeue itself for the remainder of the work to be rescheduled at some future time. 根据你的描述,我猜你想要的是一个需要睡觉才能实际停止的任务,保存进度信息,然后重新安排自己以便将来重新安排的其余工作。 You'd have to build this into your application architecture. 您必须将其构建到您的应用程序架构中。

Alternatively, you could have the scheduler threads launch the worker tasks in their own separate threads, letting them sleep as necessary, with one scheduler thread collecting all the worker terminations. 或者,您可以让调度程序线程在其自己的单独线程中启动工作任务,让它们在必要时休眠,一个调度程序线程收集所有工作程序终止。

To get a better answer you're going to have to provide more information about what you're trying to accomplish. 为了获得更好的答案,您将不得不提供有关您要完成的内容的更多信息。

Tasks which sleep are inherently unfriendly for running in any kind of bounded thread pool. 睡眠的任务对于在任何类型的有界线程池中运行本质上是不友好的。 The sleep is explicitly telling the thread that it must do nothing for a period of time. 睡眠明确地告诉线程它在一段时间内必须什么都不做。

If possible, split the task into 2 (or more parts), eliminating the sleep completely. 如果可能,将任务分成2个(或更多部分),完全消除睡眠。 Get the first half-task to schedule the second task with an appropriate delay. 获得上半任务,以适当的延迟安排第二项任务。

Failing that, you could consider increasing the size of your thread pool somewhat - either setting a much larger cap to its size, or possibly even eliminating the cap altogether (not recommended for a server than might end up with many clients). 如果做不到这一点,您可以考虑稍微增加线程池的大小 - 或者设置一个更大的上限到其大小,或者甚至可以完全取消上限(不推荐用于服务器而不是最终可能有许多客户端)。

Alternatively, move the tasks with sleep statements in them into their own Scheduled executor. 或者,将带有sleep语句的任务移动到它们自己的Scheduled执行程序中。 Then, they'll delay each other, but better-behaved tasks, with no wait statements in them, will get preferential treatment. 然后,他们会相互推迟,但是没有等待陈述的表现更好的任务将获得优惠待遇。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM