java invokeall 阻塞_java - ExecutorService invokeall不会被中断 - 堆栈内存溢出

我有以下代码:

public class Application1 {

public static void main(String[] args) {

ExecutorService executorService = Executors.newFixedThreadPool(5);

List> callableTasks = new ArrayList<>();

callableTasks.add(new LogDownloadCallable());

callableTasks.add(new LogDownloadCallable());

callableTasks.add(new LogDownloadCallable());

callableTasks.add(new LogDownloadCallable());

callableTasks.add(new LogDownloadCallable());

List> futures =null;

try {

futures = executorService.invokeAll(callableTasks, 1090, TimeUnit.MILLISECONDS);

} catch (InterruptedException e) {

System.out.println("invokeall inturrupted !!");

}

executorService.shutdownNow();

}

}

class LogDownloadCallable implements Callable {

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;

}

}

我希望在1090毫秒超时后得到"java.lang.InterruptedException" 。 但这不会发生。 有人可以帮助我理解为什么吗? 如果我放一个Thread.sleep(2000); 在public Boolean call() throws Exception {的try块中,在for循环之前public Boolean call() throws Exception { ,然后得到了InterruptedException。 这种行为很奇怪。 PS:这只是我用来说明问题的一个虚构示例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值