
import java.io.PrintStream;
import java.util.*;
import java.util.concurrent.*;

public class Main
{
    static PrintStream o=System.out;
	public static  
	void main(String[] args) 
	throws Exception
	{
		Future f=execute ( 1,TimeUnit.SECONDS, new Callable (){

			@Override
			public Object call ()
			throws Exception
			{
				for(int i=0;;++i)
					if (i % 100000000==0)
						o.println ( "working hard..." );
			}			
		});
		o.println ( "Cancel!" ); 
        f.cancel ( false ); //ты больше не нужен
		f.cancel ( true ); //да останавливайся сука
		executor.shutdownNow (); //ну пожалуйста - освободи ценные ресурсы
        o.println ( "Shutdown" ); 
		
	}
	@SuppressWarnings("unchecked")
	public static <T> 
	Future<T> execute(
		long timeout,TimeUnit unit, Callable<T> task
	){
		return executeAll(timeout,unit,Arrays.asList ( task)).get(0);
	}    
		
	public static <T> 
	List<Future<T>> executeAll(
		long timeout, TimeUnit unit,Collection<Callable<T>> tasks
	){
		try{
			return executor.invokeAll(tasks, timeout, unit);
		}catch (InterruptedException e){
			throw new RuntimeException(e);
		}		 
	}
	static void sl(int millis){
		try {
			Thread.sleep (millis);
		}catch (InterruptedException e) {
			e.printStackTrace();
		}
	}	
	final static ExecutorService 
		executor = Executors.newFixedThreadPool ( 3 );
}
