Class ShutdownHooks

java.lang.Object
org.jline.utils.ShutdownHooks

public final class ShutdownHooks extends Object
Manages the JLine shutdown-hook thread and tasks to execute on shutdown.

The ShutdownHooks class provides a centralized mechanism for registering tasks that should be executed when the JVM shuts down. It manages a single shutdown hook thread that executes all registered tasks in the reverse order of their registration.

This class is particularly useful for terminal applications that need to perform cleanup operations when the application is terminated, such as restoring the terminal to its original state, closing open files, or releasing other resources.

Tasks are registered using the add(Task) method and can be removed using the remove(Task) method. All tasks must implement the ShutdownHooks.Task interface, which defines a single ShutdownHooks.Task.run() method that is called when the JVM shuts down.

Example usage:

 // Create a task to restore the terminal on shutdown
 ShutdownHooks.Task task = ShutdownHooks.add(() -> {
     terminal.setAttributes(originalAttributes);
     terminal.close();
 });

 // Later, if the task is no longer needed
 ShutdownHooks.remove(task);
 
Since:
2.7