开发者

Dynamic Thread Pool Management

开发者 https://www.devze.com 2023-04-12 16:50 出处:网络
I have three types of Tasks A,B,C to be executed with minimum thresholds in a Thread Pool: (A) 70 % (B) 20 %

I have three types of Tasks A,B,C to be executed with minimum thresholds in a Thread Pool:

  • (A) 70 %
  • (B) 20 %
  • (C) 10 %

Pool Size: 100 Threads.

How to ensure in any given time the following distribu开发者_如何学Ction:

  1. No Idle Threads: If ,for example, only type C tasks exists, The pool will be 100% C's
  2. No starvation: B and C Tasks will eventually be served


You should try the following strategy:

  • maintain a PriorityBlockingQueue with all items inserted into the queue having a priority field which is the basis for comparison. Let tasks of type C have priority 0, B have 10, and A have 20 (lower values == higher priority)
  • Maintain a separate 'tracking' queue of active B items in the PriorityBlockingQueue. A separate Timer instance attempts to remove all B items from the PriorityBlockingQueue, when successful it decrements the priority for B and reinserts it into the PriorityBlockingQueue - this ensures no starvation for items of type B. Unsuccessful attempts mean the item has already been processed and that the item should be removed from the tracking queue.
  • Threads in the thread pool attempt to pop an element of the queue before working on it.
0

精彩评论

暂无评论...
验证码 换一张
取 消