Java Executors.newWorkStealingPool
June 02, 2022
On this page we will learn using Executors.newWorkStealingPool
method.
1. Work stealing is a scheduling strategy in parallel computing for multithreaded programs. In work stealing scheduler, each processor in computer system has a queue of work items. When a processor runs out of its work items, it steals the work item from the queue of other processors.
2. The
newWorkStealingPool
creates the work stealing thread pool using parallelism. By default it uses number of available processors as parallelism number. We can also pass our parallelism number as an argument to this method.
3. The number of threads in work stealing thread pool may shrink or grow dynamically. The maximum number of threads corresponds to the number of parallelism.
4. Find the
newWorkStealingPool
method declarations.
a. With given parallelism.
static ExecutorService newWorkStealingPool(int parallelism)
The parallelism parameter is the targeted parallelism level.
It returns the newly created thread pool.
If parallelism <= 0, it throws
IllegalArgumentException
.
b.
static ExecutorService newWorkStealingPool()
It returns the newly created thread pool.
Example-1
Find thenewWorkStealingPool
example with default parallelism.
ExecutorDemo1.java
package com.concretepage; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class ExecutorDemo1 { public static void main(final String... args) throws InterruptedException { Runnable task1 = () -> System.out.println("Executing Task-1 ..."); Runnable task2 = () -> System.out.println("Executing Task-2 ..."); ExecutorService execService = Executors.newWorkStealingPool(); int availableProcessors = Runtime.getRuntime().availableProcessors(); System.out.println("availableProcessors: " + availableProcessors); execService.submit(task1); execService.submit(task2); execService.awaitTermination(3, TimeUnit.SECONDS); execService.shutdownNow(); } }
availableProcessors: 4 Executing Task-1 ... Executing Task-2 ...
Example-2
Find thenewWorkStealingPool
example with given parallelism.
ExecutorDemo2.java
package com.concretepage; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; public class ExecutorDemo2 { public static void main(final String... args) throws InterruptedException, ExecutionException { Callable<String> task1 = () -> { System.out.println("Executing Task-1"); return "Task-1 Finish."; }; Callable<String> task2 = () -> { System.out.println("Executing Task-2"); return "Task-2 Finish."; }; ExecutorService execService = Executors.newWorkStealingPool(); Future<String> future1 = execService.submit(task1); Future<String> future2 = execService.submit(task2); String output1 = future1.get(); System.out.println(output1); String output2 = future2.get(); System.out.println(output2); execService.awaitTermination(3, TimeUnit.SECONDS); execService.shutdownNow(); } }
Executing Task-1 Executing Task-2 Task-1 Finish. Task-2 Finish.