title: How to break out of a loop when no more threads are active
body: <p>I've got a program where I initialize and start a variable number of threads. I need the main program to interact with the threads so I put those inside an infinite loop. After all the threads have died though I would like a way to break the loop. Here goes part of my code:</p>
  
  <pre><code>public static void main(String[] args) {
  //some random initializing
  
  for(int i = 1; i &lt;= num; i++){     //p1 is a Runnable
          Thread t = new Thread(p1, "t"+ Integer.toString(i));
          t.start();
  } 
  while(true){
  //do some stuff while threads are active
  //not sure what if statement to put here to break loop
  }
  </code></pre>
  
  <p>I can post more of my code if I need to, but I think it would be largely irrelevant.</p>