java - awaitDone in FutureTask throwing InterruptedException -
i've been searching web week none of posts how futuretask return after timeoutexception? seems answer question. i've extracted code sample code:
future<object> result = executorservice.submit(new callable<object>() { @override public object call() throws exception { ........... } }); try { return result.get(timeout.getvalue(), timeout.getunit().gettimeunit()); } catch (timeoutexception e) { result.cancel(true); throw new ttimeoutexception(e); }
if run this, sometimes(1 out of 1000) i'm getting
java.lang.interruptedexception @ java.util.concurrent.futuretask.awaitdone(futuretask.java:400) @ java.util.concurrent.futuretask.get(futuretask.java:199) @ com.abc.callers.a.call(a.java:83)
and line no:83 result.get() of future task shown in above code sample.
now question is, can calling result.cancel(true) in future cause interruptedexception in result.get? if not, can change interrupt status of current thread? afaik result.get() isn't same thread 1 running submitted task i'm cancelling..
from javadoc:
boolean cancel(boolean mayinterruptifrunning)
attempts cancel execution of task. attempt fail if task has completed, has been cancelled, or not cancelled other reason. if successful, , task has not started when cancel called, task should never run. if task has started,
mayinterruptifrunning
parameter determines whether thread executing task should interrupted in attempt stop task.after method returns, subsequent calls
isdone()
return true. subsequent callsiscancelled()
return true if method returned true.
so if mayinterruptifrunning
set, yes, attempt interrupt thread running task.
in other words, .cancel(true)
attempt interrupt running task, , .cancel(false)
won't.
it sounds though result.get()
still interrupted if call .cancel(false)
, task hasn't started.
Comments
Post a Comment