Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Future.get() gets interrupted always with an InterruptedException

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..

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

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

Here is the code:

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();
    }

And the output

 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);
    }

and afterwards I iterate over this Set with

    for(Future<HashMap<Integer, Agent>> f : agentFutureSet)
    try
    {
              //Here goes the code at the beginning
like image 201
dgrandes Avatar asked Nov 24 '10 15:11

dgrandes


2 Answers

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.

like image 51
daveb Avatar answered Sep 22 '22 01:09

daveb


A bare InterruptedException being thrown from .get() indicates that the current thread of execution (the thread calling .get()) was interrupted before calling get(), or while blocked in .get(). This is not the same thing as an executor thread or one of its tasks being interrupted. Someone or something is interrupting your "main" thread -- or at least the thread calling .get().

Interruptions do not happen randomly -- some code has to intentionally cause the interruption. An interrupt can only be initiated by a call to Thread.interrupt(), but there are a few standard Java utilities that call this behind the scenes, like Future.cancel(true) and ExecutorService.shutdownNow().

AFAIK, there is no way of externally tracking the "cause chain" or "stack trace" of the cause of interruption. You must look to the source code to determine where interruptions can be initiated, and thus deduce what method call(s) are causing the interruption in your case.

like image 26
Mike Clark Avatar answered Sep 19 '22 01:09

Mike Clark