Java Executors.newWorkStealingPool Example

By Arvind Rai, 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) 
It creates a thread pool that maintains enough threads to support the given parallelism level. It may use multiple queues to reduce contention. The number of threads in pool corresponds to the given parallelism number. The maximum number of threads actively engaged in, or available to engage depends on the given parallelism. The number of threads in the pool may increase or decrease dynamically. This thread pool makes no guarantees about the order in which submitted tasks are executed.
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 creates a work-stealing thread pool using the number of available processors as its target parallelism level.
It returns the newly created thread pool.

Example-1

Find the newWorkStealingPool 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();
  }
} 
Output
availableProcessors: 4
Executing Task-1 ...
Executing Task-2 ... 

Example-2

Find the newWorkStealingPool 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();
  }
} 
Output
Executing Task-1
Executing Task-2
Task-1 Finish.
Task-2 Finish. 

Reference

Class Executors
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us