Java の Future.get() に奇妙な問題があります。常に InterruptedException で返されますが、奇妙なことに、例外の原因が null であるため、誰が私を中断したのかわかりません..
get() を呼び出す前にチェックし、Future が実行しなければならない仕事は既に完了しているため、さらに悪化します。
以下の出力を担当するコードを次に示します。f は Futureであり、 callable は Agent が実際には関連しない HashMap を返します。印刷行が多すぎる場合は申し訳ありませんが、できるだけ多くの情報を提供しようとしています。callable からの call メソッドは、今のところ単純なSystem.out.println("Hola soy agente")
もので、後でわかるように出力されます。つまり、callable が例外を引き起こさなかったことを意味します。
コードは次のとおりです。
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)
呼び出し可能オブジェクトをスレッドプールに送信する場所を確認したい場合は、これがそのコードになります
for(Callable<HashMap<Integer, Agent>> c : agentCallables)
{
Future<HashMap<Integer,Agent>> future = pool.submit(c);
agentFutureSet.add(future);
}
その後、この Set を次のように反復処理します
for(Future<HashMap<Integer, Agent>> f : agentFutureSet)
try
{
//Here goes the code at the beginning