Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're right that there's a race there. <code>FutureTask#done()</code> will be called <em>at most once</em>, so if the task has already been canceled before it was ever run via <code>RunnableFuture#run()</code>, you'll have missed the call to <code>FutureTask#done()</code>.</p> <p>Have you considered a simpler approach that always issues a symmetric set of paired calls to <code>ITaskStatusHandler#taskRunning()</code> and <code>ITaskStatusHandler#taskCompleted()</code>, like so?</p> <pre><code>@Override public void run() { statusHandler.TaskRunning(this); try { super.run(); finally { statusHandler.TaskCompleted(this); } } </code></pre> <p>Once <code>RunnableFuture#run()</code> is called, it's true that your task in running, or at least trying to be run. Once <code>FutureTask#run()</code> is done, your task is no longer running. It so happens that in the case of cancellation, the transition is (nearly) immediate.</p> <p>Trying to avoid calling <code>ITaskStatusHandler#taskRunning()</code> if the inner <code>Callable</code> or <code>Runnable</code> is never invoked by <code>FutureTask#run()</code> will require you to establish some shared structure between the <code>Callable</code> or <code>Runnable</code> and the <code>FutureTask</code>-derived type itself, so that when your inner function is first called you set some flag that the outer <code>FutureTask</code>-derived type can observe as a latch, indicating that yes, the function did start running <em>before</em> having been canceled. However, by then, you had to have committed to calling <code>ITaskStatusHandler#taskRunning()</code>, so the distinction isn't so useful.</p> <p>I've been struggling with a similar design problem lately, and wound up settling on the symmetric <em>before</em> and <em>after operations</em> in my overridden <code>FutureTask#run()</code> method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload