Skip to content

io microsphere concurrent ExecutorUtils

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

ExecutorUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.concurrent | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/concurrent/ExecutorUtils.java

Overview

Executor Utilities class

Declaration

public abstract class ExecutorUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

shutdownOnExit

ExecutorService executor1 = Executors.newFixedThreadPool(2);
ExecutorService executor2 = Executors.newSingleThreadExecutor();

ExecutorUtils.shutdownOnExit(executor1, executor2);

shutdown

ExecutorService executor = Executors.newFixedThreadPool(2);
boolean isShutdown = ExecutorUtils.shutdown(executor);
System.out.println("Executor shutdown: " + isShutdown); // Output: true
Executor nonServiceExecutor = (runnable) -> new Thread(runnable).start();
boolean isShutdown = ExecutorUtils.shutdown(nonServiceExecutor);
System.out.println("Executor shutdown: " + isShutdown); // Output: false

shutdown

ExecutorService executor = Executors.newFixedThreadPool(2);
boolean isShutdown = ExecutorUtils.shutdown(executor);
System.out.println("Executor shutdown: " + isShutdown); // Output: true
ExecutorService alreadyShutdownExecutor = Executors.newSingleThreadExecutor();
alreadyShutdownExecutor.shutdown(); // manually shutting down
boolean result = ExecutorUtils.shutdown(alreadyShutdownExecutor);
System.out.println("Executor shutdown: " + result); // Output: false

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.concurrent.ExecutorUtils;

API Reference

Public Methods

Method Description
shutdownOnExit Registers a shutdown hook to gracefully shut down the given Executor instances when the JVM exits.
shutdown Attempts to shut down the given Executor if it is an instance of ExecutorService.
shutdown Attempts to shut down the given ExecutorService gracefully.

Method Details

shutdownOnExit

public static void shutdownOnExit(Executor one, Executor... others)

Registers a shutdown hook to gracefully shut down the given Executor instances when the JVM exits.

This method adds a JVM shutdown hook using ShutdownHookUtils#addShutdownHookCallback(Runnable), ensuring that all provided executors are shut down properly upon application exit.

Example Usage

`ExecutorService executor1 = Executors.newFixedThreadPool(2);
ExecutorService executor2 = Executors.newSingleThreadExecutor();

ExecutorUtils.shutdownOnExit(executor1, executor2);
`

shutdown

public static boolean shutdown(Executor executor)

Attempts to shut down the given Executor if it is an instance of ExecutorService.

If the provided Executor is an instance of ExecutorService, this method will delegate the shutdown process to the #shutdown(ExecutorService) method. Otherwise, no action is taken and the method returns false.

Example Usage

`ExecutorService executor = Executors.newFixedThreadPool(2);
boolean isShutdown = ExecutorUtils.shutdown(executor);
System.out.println("Executor shutdown: " + isShutdown); // Output: true
`
`Executor nonServiceExecutor = (runnable) -> new Thread(runnable).start();
boolean isShutdown = ExecutorUtils.shutdown(nonServiceExecutor);
System.out.println("Executor shutdown: " + isShutdown); // Output: false
`

shutdown

public static boolean shutdown(ExecutorService executorService)

Attempts to shut down the given ExecutorService gracefully.

This method checks if the provided ExecutorService is not already shutdown. If it is still active, this method initiates an orderly shutdown by calling ExecutorService#shutdown(). If the executor is already shutdown or null, no action is taken and the method returns false.

Example Usage

`ExecutorService executor = Executors.newFixedThreadPool(2);
boolean isShutdown = ExecutorUtils.shutdown(executor);
System.out.println("Executor shutdown: " + isShutdown); // Output: true
`
`ExecutorService alreadyShutdownExecutor = Executors.newSingleThreadExecutor();
alreadyShutdownExecutor.shutdown(); // manually shutting down
boolean result = ExecutorUtils.shutdown(alreadyShutdownExecutor);
System.out.println("Executor shutdown: " + result); // Output: false
`

See Also

  • Executor
  • ExecutorService
  • Executors

This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally