I have following code :
public class Application1 {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
List<Callable<Boolean>> callableTasks = new ArrayList<>();
callableTasks.add(new LogDownloadCallable());
callableTasks.add(new LogDownloadCallable());
callableTasks.add(new LogDownloadCallable());
callableTasks.add(new LogDownloadCallable());
callableTasks.add(new LogDownloadCallable());
List<Future<Boolean>> futures =null;
try {
futures = executorService.invokeAll(callableTasks, 1090, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
System.out.println("invokeall inturrupted !!");
}
executorService.shutdownNow();
}
}
class LogDownloadCallable implements Callable<Boolean> {
public Boolean call() throws Exception {
try{
//This for sure takes days to complete, so should through Cancellation exception because timeout on invokeall set to 1 minute
long val = 0;
for (long i = 0; i < Long.MAX_VALUE - 5000; i++) {
val += i;
}
System.out.println("complete");
}catch(Exception e){
System.out.println("Exception ! " +e.toString()+ Thread.currentThread().getId());
return false;
}
return true;
}
}
I was hoping to get "java.lang.InterruptedException"
after 1090 msec timeout. But that doesn't happen. Can somebody help me understand why ?
If I put a Thread.sleep(2000);
in the try block in public Boolean call() throws Exception {
, before the for loop, then I am getting InterruptedException. This behavior is weird.
PS : this is just a dummy example I made up to showcase my problem.