1ExecutorService executor=Executors.newSingleThreadExecutor();
2Future<ReturnType> future=executor.submit(task);
3try{
4 ReturnType result=future.get(1,TimeUnit.SECONDS);
5 //task successful
6}catch(TimeoutException e){
7 //timeout
8 future.cancel(true);
9}catch(InterruptedException e){
10 //current thread was interrupted during task execution
11 Thread.currentThread().interrupt();
12}catch(ExecutionException e){
13 //task threw Exception
14 throw e.getCause();
15}