Future.get () is always interrupted with an InterruptedException

I have a WEIRD problem with Future.get () in Java. It always returns with an InterruptedException, however it is strange that the cause of the exception is null, so I canโ€™t say who interrupted me ..

This gets even worse, because I check before calling get (), and the work that Future should do is already done.

Below is the code responsible for the output below. f is the future , and the caller returns a HashMap, where the agent has no special meaning. Sorry if there are too many lines of print, I'm just trying to give as little information as possible. The call method from the called now is a simple System.out.println("Hola soy agente") which, as you will see, is printed, which means that the called did not raise an 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 exit

  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 suppress the threadpool called thread ... then that 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 after that I repeat this set with

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

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

For more information, look at javadoc for Future.get () to see why it will throw an InterruptedException.

+9
source

A bare InterruptedException .get() from .get() indicates that the current thread of execution (the thread that calls .get() ) was interrupted before calling get() or blocked in .get() . This is not the same as the flow of artists or one of its tasks is interrupted. Someone or something interrupts your "main" thread - or at least the thread calling .get() .

Interrupts do not happen randomly - some code must intentionally cause an interrupt. An interrupt can only be triggered by a call to Thread.interrupt() , but there are several standard Java utilities that call this backstage, such as Future.cancel(true) and ExecutorService.shutdownNow() .

AFAIK, there is no way to externally track the "reason chain" or "stack trace" of the cause of the interrupt. You should look at the source code to determine where interrupts can be triggered, and thus deduce which calls (calls) caused the interrupt in your case.

+5
source

The reason for the exception is Throwable (a method call is no exception)

The thread will only be interrupted if you call it, it will not happen by accident.

If you really don't know where the interrupt is coming from and you want to ignore it, you can clear it first. call Thread.interrupted()

+1
source

This indicates that the thread executing the transmitted Callable is interrupted. This can be an interrupt using graphite (for example, if Callable performs some blocking operation, such as Thread.sleep, and is explicitly interrupted through Thread.interrupt() ), or it may be that ExecutorService (provided that you use this ) is called using shutDownNow() .

Can you send the code used to create the thread pool along with the Callable implementation?

0
source

All Articles