java - Interrupting a thread prior to calling Future.get() -
java - Interrupting a thread prior to calling Future.get() -
i'm trying write integration test causes interruptedexception
raised production code:
@test public void test() { productionobject = new productionobject( com.google.common.util.concurrent.moreexecutors.samethreadexecutor()); thread.currentthread().interrupt(); assertthat(productionobject.execute(), equalto(defaultresponse)); }
inside productionobject
's implementation:
try { (future<t> future : executorservice.invokeall(tasks))) { results.add(future.get()); } homecoming results; } grab (interruptedexception e) { thread.currentthread().interrupt(); // preserve interrupt flag homecoming defaultresponse; }
inside abstractqueuedsynchronizer.acquiresharedinterruptibly()
see:
if (thread.interrupted()) throw new interruptedexception();
so expect test pass consistently.
i've seen fail in our build server (results
returned rather defaultresponse
). i've been unable reproduce failure locally, running test in while (true) loop, , simulating higher load running glxgears software rendering ;-) can spot mistake, give me suggestions on look, or suggest tools help me?
strange. read code same way do. see:
futuretask.get()
calls sync.get()
. assume dealing futuretask
here. sync.get()
calls sync.innerget()
sync.innerget()
calls acquiresharedinterruptibly(0)
; which has code right off:
if (thread.interrupted()) throw new interruptedexception();
i think throw. maybe there sort of race status thread not yet know has been interrupted? have tried sleep 100ms after interrupt thread?
i ran next test on multi-cpu mac , never fails not race status -- @ to the lowest degree architecture , jre version 1.6.0_41.
for (long = 0; < 10000000; i++) { thread.currentthread().interrupt(); asserttrue(thread.interrupted()); }
java multithreading interrupt future
Comments
Post a Comment