Class ShutdownHooks


  • public final class ShutdownHooks
    extends java.lang.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. By default, it manages a single shutdown hook thread that executes all registered tasks in the 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
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  ShutdownHooks.Task
      Essentially a Runnable which allows running to throw an exception.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String PROP_DISABLE_SHUTDOWN_HOOK
      System property that, when set to true, prevents JLine from registering its shutdown hook with the JVM.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends ShutdownHooks.Task>
      T
      add​(T task)
      Adds a task to be executed when the JVM shuts down.
      static void remove​(ShutdownHooks.Task task)  
      static void runTasks()
      Runs all registered tasks, in the order they were registered, then forgets them.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PROP_DISABLE_SHUTDOWN_HOOK

        public static final java.lang.String PROP_DISABLE_SHUTDOWN_HOOK
        System property that, when set to true, prevents JLine from registering its shutdown hook with the JVM. Tasks are still tracked, and the application is then responsible for calling runTasks() itself, for instance from its own shutdown hook.
        See Also:
        Constant Field Values
    • Method Detail

      • add

        public static <T extends ShutdownHooks.Task> T add​(T task)
        Adds a task to be executed when the JVM shuts down.

        This method registers a task to be executed when the JVM shuts down. Tasks are executed in the order of their registration, so the first added task will be executed first.

        If this is the first task to be added, a shutdown hook thread will be created and registered with the JVM. This thread will execute all registered tasks when the JVM shuts down.

        Type Parameters:
        T - the type of the task
        Parameters:
        task - the task to be executed on shutdown
        Returns:
        the task that was added (for method chaining)
        Throws:
        java.lang.NullPointerException - if the task is null
      • runTasks

        public static void runTasks()
        Runs all registered tasks, in the order they were registered, then forgets them.

        This is what the JLine shutdown hook does. It is meant for applications that set PROP_DISABLE_SHUTDOWN_HOOK and decide themselves when terminals get closed, for instance after their logging has been flushed. A task that fails is logged and does not prevent the other tasks from running.