Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling tasks with different priorities in a thread pool

I have many incoming tasks of priority A, B and C and I want to handle the tasks with a thread pool on a multicore CPU. 70% of the CPU should be used to process 'type A' tasks, 20% of the CPU for 'type B' tasks and 10% of the CPU for 'type C' tasks.

If however, only tasks of 'type C' arrive, then 100% of the CPU should be devoted to them. If only task B and C arirve then 66% will proccess task B and 33% task C, etc...

How would you implement this in Java?

p.s: A priority queue won't work because then only type a tasks will be processed. Also, assigning priorities to threads wont work because its not accurate.

like image 826
dov.amir Avatar asked Mar 10 '12 13:03

dov.amir


People also ask

Does task class use thread pool?

When you create a Task or Task<TResult> object to perform some task asynchronously, by default the task is scheduled to run on a thread pool thread. Asynchronous timers use the thread pool. Thread pool threads execute callbacks from the System.

Which issue could be solved by thread pool?

A thread pool helps mitigate the issue of performance by reducing the number of threads needed and managing their lifecycle. Essentially, threads are kept in the thread pool until they're needed, after which they execute the task and return the pool to be reused later.

When using thread pool what happens?

Once a thread in the thread pool completes its task, it's returned to a queue of waiting threads. From this moment it can be reused. This reuse enables applications to avoid the cost of creating a new thread for each task. There is only one thread pool per process.


2 Answers

Maybe you should use 3 pools of threads then. 1 pool of 7 threads for A tasks, 1 pool of 2 threads for B tasks, and 1 pool of 1 thread for C tasks.

EDIT: to have parallelism even if there are only C tasks (or, if you have many processors, if you only have B and C tasks), multiply the number of threads in each pool by the number of processors, or the number of processors + 1, or any other bigger factor you like.

like image 65
JB Nizet Avatar answered Oct 17 '22 18:10

JB Nizet


this is actually a fairly difficult problem. basically, you would need to implement a custom blocking queue which tracks not only the pending jobs, but also the currently running jobs. for the pending jobs, i would keep separate lists for the various priority levels. additionally, you should keep track of the running counts for each priority level. when a new job is requested from the queue, you need to check the ratio of currently running jobs and choose the next available job from the highest applicable priority (deferring to other priorities if none at the desired priority are available). obviously, you will need feedback into this queue for your running jobs, so you will probably want to wrap the actual jobs with a wrapper Runnable which runs the real job and then updates your queue when that job is finished.

like image 25
jtahlborn Avatar answered Oct 17 '22 17:10

jtahlborn