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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With