Code example for FutureTask

Methods: cancel, sleep

0
	/** 
	 * @param args 
	 */ 
	public static void main(String[] args)
	{ 
		FutureTask<Void> task = new FutureTask<Void>(new Callable<Void>() {
 
			boolean keepRunning = true;
			 
			@Override 
			public Void call() throws Exception
			{ 
				return null; 
			} 
			 
			public void run() 
			{ 
				while (keepRunning)
				{ 
					System.out.println("gagaga");
				} 
			} 
			 
			public boolean cancel(boolean b)
			{ 
				keepRunning = false;
				return true; 
			} 
			 
		}); 
		 
		ExecutorService es = Executors.newFixedThreadPool(1);
		Future<?> f = es.submit(task);
		 
		try 
		{ 
			Thread.sleep(1000);
		} 
		catch (InterruptedException e)
		{ 
			// TODO Auto-generated catch block 
			e.printStackTrace();
		} 
		task.cancel(true);
 
	} 
 
}