1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| private static void testTimeout() throws Exception {
ExecutorService executor = Executors.newCachedThreadPool();
Callable<Object> task = new Callable<Object>() {
public Object call() throws InterruptedException, MalformedURLException, Exception {
return compte(2000000000);
}
};
Future<Object> future = executor.submit(task);
try {
System.out.println("Avant appel : "+ new Date());
Object result = future.get(100, TimeUnit.MILLISECONDS);
System.out.println("Apres appel : "+ new Date());
}catch (TimeoutException te){
future.cancel(true);
executor.shutdownNow();
throw new Exception("TimeoutException : cette action a pris plus que le temps permi");
}catch(ExecutionException ee){
future.cancel(true);
executor.shutdownNow();
throw new Exception("ExecutionException : Une erreur d'execution s'est produite");
}catch(InterruptedException ie){
future.cancel(true);
executor.shutdownNow();
throw new Exception("InterruptedException : Une erreur a servenu");
}
} |
Partager