Skip to content
Advertisement

How to set the Thread priority on parallel iteration with standard Java API?

I found in this tutorial a way to iterate in parallel a collection of objects by setting threads priority, but this example uses the Burningwave Core library: is there a way to do the same with the standard Java API?

I found in this tutorial a way to iterate in parallel a collection of objects by setting threads priority, but this example uses the Burningwave Core library: is there a way to do the same with the standard Java API?

Here the code of the tutorial:

JavaScript

Advertisement

Answer

Referring to the answer from Custom thread pool in Java 8 parallel stream You can use List.parallelStream with a Custom ForkJoinPool. The trick is fill up the pool with a custom ForkJoinWorkerThreadFactory that sets required priority to threads.

  1. Create a custom ForkJoinWorkerThreadFactory that sets the required thread priority.
  2. Create a ForkJoinPool by passing this factory in constructor.
  3. Submit the parallel stream task as explained in the above mentioned answer.
JavaScript
Advertisement