Future.get()总是被InterruptedException中断 [英] Future.get() gets interrupted always with an InterruptedException

查看:256
本文介绍了Future.get()总是被InterruptedException中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中的Future.get()有一个WEIRD问题.它总是返回一个InterruptedException,但是很奇怪的是,异常的原因是null,所以我不能说出是谁打断了我.

I have a WEIRD problem with Future.get() in Java. It returns always with an InterruptedException, however the weird thing is that the cause of the Exception is null, so I cant tell who interrupted me..

情况变得更糟,因为我在调用get()之前进行了检查,而Future要做的工作已经完成.

It gets even worse because I check before calling get(), and the job Future has to do is already done.

这是负责以下输出的代码. f是Future ,并且可调用对象返回一个与Agent无关的HashMap.很抱歉,如果打印行太多,我只是想提供尽可能多的信息.现在,来自callable的call方法是一个简单的 System.out.println("H​​ola soy agente"),它会被打印出来,这意味着该callable也不引起异常

Here is the code responsible for the output below. f is the Future, and the callable returns a HashMap where Agent is not really relevant. Sorry if there are too many printlines, I'm just trying to give as mush info as I can. The call method from callable is for now a simple System.out.println("Hola soy agente") that as you will see, gets printed, meaning that the callable didn't cause the exception either

这是代码:

try
    {
        System.out.println(f.isDone());        //true
        System.out.println(f.isCancelled());   //false
        System.out.println(f.toString());      //FutureTask
        newModdedAgents.putAll(f.get());
    }catch(InterruptedException e)
    {
        System.out.println(f.isDone());        //true
        System.out.println(f.isCancelled());   //false
        System.err.println(e);                 //It is an interruptedException
        System.err.println(e.getCause());     //???? null?
        e.printStackTrace();
    }

和输出

 Hola soy agente
 true
 false
 java.util.concurrent.FutureTask@1c4c94e5
 true
 false
 java.lang.InterruptedException
 null

java.lang.InterruptedException
at     java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1302)
at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:248)
at java.util.concurrent.FutureTask.get(FutureTask.java:111)
at com.pf.simulator.Simulation.simulateStep(Simulation.java:217)
at com.pf.gui.ButtonPanel.doWork(ButtonPanel.java:141)
at com.pf.gui.ButtonPanel$1$1.construct(ButtonPanel.java:198)
at com.pf.gui.SwingWorker$2.run(SwingWorker.java:117)
at java.lang.Thread.run(Thread.java:636)

如果您想看到我将可调用对象汇总到线程池中,那么这就是它的代码

In case you want to see where I sumbit the callable to the threadpool... then this would be the code for it

    for(Callable<HashMap<Integer, Agent>> c : agentCallables)
    {
        Future<HashMap<Integer,Agent>> future = pool.submit(c);
        agentFutureSet.add(future);
    }

然后我用

    for(Future<HashMap<Integer, Agent>> f : agentFutureSet)
    try
    {
              //Here goes the code at the beginning

推荐答案

在调用 get()之前是否检查了线程的中断标志?您可以使用 Thread.currentThread().isInterrupted()来做到这一点.

Did you check the thread's interrupt flag before calling get()? You can do this with Thread.currentThread().isInterrupted().

有关更多信息,请参见

For more info look at the javadoc for Future.get() to see why it would throw an InterruptedException.

这篇关于Future.get()总是被InterruptedException中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆