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:
- No Idle Threads: If ,for example, only type C tasks exists, The pool will be 100% C's
- 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.
精彩评论