Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Thanks to each one of you for responding and also for pointing out the obvious bloopers in the code I pasted. I will give a context of the problem I am solving.</p> <p>I will receive requests (each request will have a set of task id's); I have a thread pool which would be used to submit this request (batch of tasks for each request). I will accept from the clients listeners that would be invoked after task completion. In the current context, I could have used an <code>ExecutorCompletionService</code> to wait for all the tasks corresponding to a request to complete, however, I do not wish to make any blocking calls.</p> <p>Instead I wish to subclass <code>FutureTask</code> (override <code>done()</code> method) and this would be instantiated in <code>newTaskFor()</code> of my subclasses executor service. All the future tasks corresponding to one request will share the <code>TaskCompletionListener</code>, and in the override <code>done()</code> method will invoke one of <code>success()</code>, <code>error()</code> or cancel. The <code>TaskCompletionListener</code>, will be initialized with the initial set of task id (number of tasks) and for each invocation will decrement the count by one after which the completion would be signaled.</p> <p>Here is a modified version of the code, I hope I haven't committed much mistakes this time around (and yes, thanks once again for all the answers last time around).</p> <pre><code>public class TaskCompletionListener implements ITaskCompletionListener { private final AtomicInteger taskCount; private final IProcessCompletionListener completionListener; public TaskCompletionListener(final HashSet&lt;String&gt; pTaskIds, IProcessCompletionListener pCompletionListener) { taskCount = new AtomicInteger(pTaskIds.size()); completionListener = pCompletionListener; } // invoked concurrently @Override public void onCancel(String pTaskId) { checkCompletion(); } // invoked concurrently @Override public void onError(String pTaskId) { checkCompletion(); } // invoked concurrently @Override public void onSuccess(String pTaskId) { checkCompletion(); } private void checkCompletion() { // method level confinement int currentCount = taskCount.decrementAndGet(); if (currentCount == 0) { completionListener.onProcessComplete(); } } interface IProcessCompletionListener { void onProcessComplete(); } } </code></pre>
 

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