import java.util.concurrent.*;


/**
 * A Runnable that can be used to cancel a Future and shutdown the
 * ExecutorService.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class Terminator implements Runnable
{
    ExecutorService  service;
    Future<?>        future;    

    /**
     * Explicit Value Constructor.
     *
     * @param future   The Future to be cancelled.
     * @param service  The ExecutorService
     */
    public Terminator(Future<?> future, ExecutorService service)
    {
        this.future  = future;
        this.service = service;
    }
    

    /**
     * The code to execute.
     */
    public void run()
    {
        // Cancel the Future
        future.cancel(true);

        // Shutdown the ExecutorService
        try
        {
            service.shutdown();
            service.awaitTermination(2000, TimeUnit.MILLISECONDS);
        }
        catch (InterruptedException ie)
        {
        }
        finally
        {
            service.shutdownNow();
        }
    }
}
