Performing Tasks in Parallel With Different Priorities in Java
In this article take a look at assigning the right workload to the CPU by creating parallel tasks with different priorities.
Join the DZone community and get the full member experience.Join For Free
A small limitation of Java CompletableFuture is the inability to choose the priority of the thread they are assigned to, thus making it impossible to assign the desired workload to the cpu. In this situation, the BackgroundExecutor component of Burningwave Core library comes to our aid, by giving us the ability to launch different Runnables or Suppliers in a parallel way and wait for them starting or finishing. For obtaining threads the BackgroundExecutor uses the ThreadSupplier component which can be configured in the burningwave.static.properties file through the following properties:
The ThreadSupplier provides a fixed number of reusable threads indicated by the ‘thread-supplier.max-poolable-threads-count‘ property and, if these threads have already been assigned, new non-reusable threads will be created whose quantity maximum is indicated by the ‘thread-supplier.max-detached-threads-count‘ property. Once this limit is reached if the request for a new thread exceeds the waiting time indicated by the ‘thread-supplier.poolable-thread-request-timeout‘ property, the ThreadSupplier will proceed to increase the limit indicated by the ‘thread-supplier.max-detached-threads-count’ property for the quantity indicated by the ‘thread-supplier.max-detached-threads-count.increasing-step‘ property.
Resetting the ‘thread-supplier.max-detached-threads-count’ property to its initial value, will occur gradually only when there have been no more waits on thread requests for an amount of time indicated by the ‘thread-supplier.max-detached-threads-count.elapsed-time-threshold-from-last-increase-for-gradual-decreasing-to-initial-value‘ property.
Let’s see now an example on how to create tasks:
Published at DZone with permission of Mikkel Jorgensen. See the original article here.
Opinions expressed by DZone contributors are their own.