Class ShutdownHookProcessDestroyer

  • All Implemented Interfaces:
    java.lang.Runnable, ProcessDestroyer

    public class ShutdownHookProcessDestroyer
    extends java.lang.Object
    implements ProcessDestroyer, java.lang.Runnable
    Destroys all registered Processes when the VM exits.
    • Constructor Summary

      Constructors 
      Constructor Description
      ShutdownHookProcessDestroyer()
      Constructs a ProcessDestroyer and obtains Runtime.addShutdownHook() and Runtime.removeShutdownHook() through reflection.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(java.lang.Process process)
      Returns true if the specified Process was successfully added to the list of processes to destroy upon VM exit.
      boolean isAddedAsShutdownHook()
      Returns whether or not the ProcessDestroyer is registered as as shutdown hook
      boolean remove​(java.lang.Process process)
      Returns true if the specified Process was successfully removed from the list of processes to destroy upon VM exit.
      void run()
      Invoked by the VM when it is exiting.
      int size()
      Returns the number of registered processes.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ShutdownHookProcessDestroyer

        public ShutdownHookProcessDestroyer()
        Constructs a ProcessDestroyer and obtains Runtime.addShutdownHook() and Runtime.removeShutdownHook() through reflection. The ProcessDestroyer manages a list of processes to be destroyed when the VM exits. If a process is added when the list is empty, this ProcessDestroyer is registered as a shutdown hook. If removing a process results in an empty list, the ProcessDestroyer is removed as a shutdown hook.
    • Method Detail

      • isAddedAsShutdownHook

        public boolean isAddedAsShutdownHook()
        Returns whether or not the ProcessDestroyer is registered as as shutdown hook
        Returns:
        true if this is currently added as shutdown hook
      • add

        public boolean add​(java.lang.Process process)
        Returns true if the specified Process was successfully added to the list of processes to destroy upon VM exit.
        Specified by:
        add in interface ProcessDestroyer
        Parameters:
        process - the process to add
        Returns:
        true if the specified Process was successfully added
      • remove

        public boolean remove​(java.lang.Process process)
        Returns true if the specified Process was successfully removed from the list of processes to destroy upon VM exit.
        Specified by:
        remove in interface ProcessDestroyer
        Parameters:
        process - the process to remove
        Returns:
        true if the specified Process was successfully removed
      • size

        public int size()
        Returns the number of registered processes.
        Specified by:
        size in interface ProcessDestroyer
        Returns:
        the number of register process
      • run

        public void run()
        Invoked by the VM when it is exiting.
        Specified by:
        run in interface java.lang.Runnable