timeout java

Solutions on MaxInterview for timeout java by the best coders in the world

showing results for - "timeout java"
Giada
13 Apr 2017
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}