title: Java concurrency: Coordinating multiple tasks and have cancellation
body: <p>Im looking for a good solution of coordinating several multithreading tasks.</p>
  
  <p>Basically I have 2 tasks, I call <code>A</code> and <code>B</code> that need to be executed on a different thread than the main thread. </p>
  
  <p>But <code>B</code> must be started after <code>A</code> has completed. <code>A</code> and <code>B</code> themselfes contain of several parts who should run parallel, called <code>A1, A2, ... B1, B2, ...</code>.</p>
  
  <p>And there is a caller from outside, who needs restart the whole job regardless of the progress. How can I archieve that? I thought of creating some sort of boolean array holding the information if each subtask (A1, ...) has already completed and if so start B. And check every few lines of code in each method if a cancellation has already been made.
  But it seems to me, that that is not an elegant solution and that there ways to coordinate excatly this.</p>
  
  <p><img src="https://i.stack.imgur.com/3Tn3d.png" alt="enter image description here"></p>