title: Communication between java threads: stopping all threads when one finishes its task
body: <p>I have n threads running in parallel and each of them does some custom logic. However, my requirement is that when any of the threads finishes it execution, all other threads should stop execution and return.</p>
  
  <p>What is the best way to implement this ? I thought of doing this by having a shared boolean variable. When any of the threads finishes it execution, it will set the boolean. All all the threads periodically read this variable and exit if when it is set. </p>
  
  <p>Also, my custom logic is an infinite loop and as soon as i know some other thread has finished execution I want to stop execution after the current iteration.</p>
  
  <p>What is the proper way of doing this ?</p>