Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util.thread;
 
 import java.util.List;
 
 
 
 /* ------------------------------------------------------------ */
ShutdownThread is a shutdown hook thread implemented as singleton that maintains a list of lifecycle instances that are registered with it and provides ability to stop these lifecycles upon shutdown of the Java Virtual Machine
 
 public class ShutdownThread extends Thread
 {
     private static final Logger LOG = Log.getLogger(ShutdownThread.class);
     private static final ShutdownThread _thread = new ShutdownThread();
 
     private boolean _hooked;
     private final List<LifeCycle_lifeCycles = new CopyOnWriteArrayList<LifeCycle>();
 
     /* ------------------------------------------------------------ */
    
Default constructor for the singleton Registers the instance as shutdown hook with the Java Runtime
 
     private ShutdownThread()
     {
     }
     
     /* ------------------------------------------------------------ */
     private synchronized void hook()
     {
         try
         {
             if (!)
                 Runtime.getRuntime().addShutdownHook(this);
             =true;
         }
         catch(Exception e)
         {
             .ignore(e);
             .info("shutdown already commenced");
         }
     }
     
     /* ------------------------------------------------------------ */
     private synchronized void unhook()
     {
         try
         {
             =false;
             Runtime.getRuntime().removeShutdownHook(this);
         }
         catch(Exception e)
         {
             .ignore(e);
             .info("shutdown already commenced");
         }
     }
     
     /* ------------------------------------------------------------ */
    
Returns the instance of the singleton

Returns:
the singleton instance of the ShutdownThread
 
     public static ShutdownThread getInstance()
     {
         return ;
     }
 
     /* ------------------------------------------------------------ */
     public static synchronized void register(LifeCycle... lifeCycles)
     {
        ..addAll(Arrays.asList(lifeCycles));
        if (..size()>0)
            .hook();
    }
    /* ------------------------------------------------------------ */
    public static synchronized void register(int indexLifeCycle... lifeCycles)
    {
        ..addAll(index,Arrays.asList(lifeCycles));
        if (..size()>0)
            .hook();
    }
    
    /* ------------------------------------------------------------ */
    public static synchronized void deregister(LifeCycle lifeCycle)
    {
        ..remove(lifeCycle);
        if (..size()==0)
            .unhook();
    }
    /* ------------------------------------------------------------ */
    @Override
    public void run()
    {
        for (LifeCycle lifeCycle : .)
        {
            try
            {
                if (lifeCycle.isStarted())
                {
                    lifeCycle.stop();
                    .debug("Stopped {}",lifeCycle);
                }
            }
            catch (Exception ex)
            {
                .debug(ex);
            }
        }
    }
New to GrepCode? Check out our FAQ X