简体   繁体   English

防止工作缓慢接管线程池

[英]Prevent from slow job taking over a thread pool

I have a system where currently every job has it's own Runnable class and I pre defined a fixed number of threads for every job. 我有一个系统,当前每个作业都有它自己的Runnable类,并且我为每个作业预定义了固定数量的线程。

My understanding is that it is a wrong practice, because: 我的理解是,这是错误的做法,因为:

  1. You have to tailor the number of threads with respect to the machine running the process. 您必须根据运行该进程的计算机调整线程数。
  2. Each threads can only take one type of job. 每个线程只能执行一种类型的作业。

Would you agree on that? 你同意吗? (current solution is wrong) (当前解决方案是错误的)


So, I'd like to use something like Java's ThreadPool instead. 因此,我想改用Java的ThreadPool之类的东西。 I was conflicted with an argument claiming that by doing so, slow jobs will take over most of the thread pool , leaving no place to the other jobs. 我与一个论点相矛盾,该论点声称,这样做, 慢的工作将接管大部分线程池 ,而其他工作则没有任何余地。 Whereas, with the current solution, a fixed number of threads were assigned to the slow worker and it won't hurt the others. 而在当前解决方案中,将固定数量的线程分配给了慢工,它不会损害其他线程。

(Notice that you can't know a-priori if a job will be "slow") (请注意,如果工作“缓慢”,您将无法先验)


How can a system be both adaptive in the number of threads it uses, but at the same time not be bounded to the most slow job? 一个系统如何既能适应使用的线程数,又不能同时受限于最慢的工作?

You could try getting the time it takes for the job to complete (With a hand-made Timer class of sorts. Then you normalize this value by dividing this time by the maximum time any given thread has taken. Finally, you multiply this number by a fixed number which varies depending on how many threads you want running per job per second. This will be the requested amount of threads this process should be using. You can adjust that according. 您可以尝试获取完成工作所需的时间(使用手工制作的Timer类)。然后,通过将该时间除以给定线程所花费的最长时间,来标准化该值。最后,将该数字乘以一个固定的数目,该数目取决于您每个作业每秒要运行的线程数。这将是此进程应使用的请求线程数,您可以根据需要进行调整。

Edit: You can set minimum and maximum values that regulate how many threads a job is entitled to. 编辑:您可以设置最小值和最大值,以调节作业有权获得的线程数。 You could alternatively request threads from a very spacious job when another thread enters the system. 您也可以在另一个线程进入系统时从非常宽敞的作业中请求线程。

Hope that helps! 希望有帮助!

It's more of a business problem. 这更多的是业务问题。 Let's say I am a telecom operator. 假设我是电信运营商。 I bar my subscribers from making outgoing calls when they don't clear their dues. 我禁止订户在未缴清会费时拨出电话。 When they make payment I clear a flag and in a second the subscriber can make calls. 当他们付款时,我会清除一个标志,然后用户可以在一秒钟内拨打电话。 But a lot of other activities go on in my system like usage processing, billing, bill formatting etc. 但是我系统中还有许多其他活动,例如使用情况处理,计费,账单格式等。

Now let's assume I have a system wide common pool of threads and I started the billing of 50K subscribers. 现在,假设我有一个系统范围的通用线程池,并且开始了5万个订户的计费。 All my threads are now processing the relatively long running billing jobs and a huge queue is building up. 我所有的线程现在都在处理运行时间相对较长的计费工作,并且正在建立大量队列。

A poor customer now makes a payment and wants to make an urgent call. 贫穷的客户现在要付款,并且想拨打紧急电话。 But I have no thread left in my pool to clear the flag. 但是我的池中没有线程可以清除该标志。 The customer had to wait for an hour before he can make the call. 客户必须等待一个小时才能拨打电话。 That's SLA breach. 这是违反SLA的行为。

What I should have done is create separate thread pools. 我应该做的是创建单独的线程池。 If the call unblocking jobs are not very frequent and short, I can create a separate pool for it with core size 5 maybe. 如果呼叫解除阻塞作业不是很频繁且时间很短,我可以为其创建一个单独的池,核心大小可能为5。 For billing jobs I'd rather create a pool with core size 25 and max-size 30. 对于计费工作,我宁愿创建一个池,其核心大小为25,最大大小为30。

So, my system limits won't anyway exceed because I know in even the worst situation I won't have more than 30 threads. 因此,我的系统限制不会超过任何限制,因为即使在最坏的情况下,我也不会拥有超过30个线程。

This will also make it easy to debug. 这也将使其易于调试。 If I have a different thread name pattern for each pool amd my system has some issues. 如果每个池和每个池都有不同的线程名称模式,则系统会出现问题。 I can easily take a thread dump and understand if the billing or the payment stuff is the culprit. 我可以轻松地进行线程转储,了解帐单或付款方式是否是罪魁祸首。

So, I think the existing design is based on some business use case which you need to thoroughly understand before proposing a solution. 因此,我认为现有设计基于一些业务用例,在提出解决方案之前您需要彻底了解这些用例。

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

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