Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Submit returns a Future object that can be used to wait on completion of tasks. You could keep track of these and add a method that recursively blocks until all subtasks are done. This way you can reuse the executor wherever you need to.</p> <pre><code>import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicBoolean; public class JobExecutor { ExecutorService executorService = Executors.newFixedThreadPool(1); private class Task implements Runnable { private final String name; private final Task[] subtasks; private final ExecutorService executorService; private volatile boolean started = false; private Future&lt;?&gt; taskFuture; // Separate list from subtasks because this is what you'll probably // actually use as you may not be passing subtasks as constructor args private final List&lt;Task&gt; subtasksToWaitOn = new ArrayList&lt;Task&gt;(); public Task(String name, ExecutorService executorService, Task... subtasks) { this.name = name; this.executorService = executorService; this.subtasks = subtasks; } public synchronized void start() { if (!started) { started = true; taskFuture = executorService.submit(this); } } public synchronized void blockTillDone() { if (started) { try { taskFuture.get(); } catch (InterruptedException e) { // TODO Handle } catch (ExecutionException e) { // TODO Handle } for (Task subtaskToWaitOn : subtasksToWaitOn) { subtaskToWaitOn.blockTillDone(); } } else { // TODO throw exception } } @Override public void run() { for (Task subtask : subtasks) { subtask.start(); subtasksToWaitOn.add(subtask); } System.out.println("My name is: " + name); } } void testSubmit() { Task subsubTask1 = new Task("Subsubtask1", executorService); Task subtask1 = new Task("Subtask1", executorService, subsubTask1); Task subtask2 = new Task("Subtask2", executorService); Task subtask3 = new Task("Subtask3", executorService); Task job = new Task("Job", executorService, subtask1, subtask2, subtask3); job.start(); job.blockTillDone(); System.out.println("Job done!"); } public static void main(String[] args) { new JobExecutor().testSubmit(); } } </code></pre> <p>Prints out:</p> <pre><code>My name is: Job My name is: Subtask1 My name is: Subtask2 My name is: Subtask3 My name is: Subsubtask1 Job done! </code></pre>
    singulars
    1. This table or related slice is empty.
    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