title: Managing a Process inside a Thread
body: <p>I'm creating a graphical interface that runs some threads.
  Each one of these threads launch a Java Process to manage a VNC connection.
  All i want is to keep track of the process lifecycle by storing it in some variables of the thread that manages. Finally, the GUI communicates with the Thread to know about Process status.</p>
  
  <p>Here is a snippet of my code:</p>
  
  <pre><code>public class VNCViewer extends Thread{
      private static final String cmd = "some command";
      private Process vnc;
      private boolean active = false ;
  
      public void run(){
          try {
              launchVNC();
          } catch (IOException ex) {
              Logger.getLogger(VNCViewer.class.getName()).log(Level.SEVERE, null, ex);
          } catch (InterruptedException ex) {
              Logger.getLogger(VNCViewer.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
      private void launchVNC() throws IOException, InterruptedException{
          if (condition){
              vnc = Runtime.getRuntime().exec(cmd);
              active = true;
              while(vnc.isAlive()){} //while the process is alive, the thread awaits
              active = false;
          }
      }
  
      public boolean isActive(){
          return active;
      }
  }
  </code></pre>
  
  <p>What happens at runtime is that the thread skips the "while" loop (I've tried by inserting a system.out.println inside the loop and it is printed only when the thread is terminated), with the result that the variable "active" is always on "false".</p>