title: Wait until child threads completed : Java
body: <p><strong><em>Problem description : -</em></strong></p>
  
  <p><em>Step 1:</em> Take input FILE_NAME from user at main thread.</p>
  
  <p><em>Step 2:</em>  Perform 10 operations on that file (i.e count chars, count lines etc.. ), and all those 10 operations must be in septate threads. It means there must be 10 child threads.</p>
  
  <p><em>Step 3:</em> Main thread waits until all those child threads completed.</p>
  
  <p><em>Step 4:</em> Print result.</p>
  
  <p><strong><em>What I did :-</em></strong></p>
  
  <p>I did a sample code with 3 threads. <strong>I don't want file operation code from your side.</strong> </p>
  
  <pre><code>public class ThreadTest {
      // This is object to synchronize on.
      private static final Object waitObject = ThreadTest.class;
      // Your boolean.
      private static boolean boolValue = false;
  
      public final Result result = new Result();
  
      public static void main(String[] args) {
          final ThreadTest mytest = new ThreadTest();
  
          System.out.println("main started");
  
          new Thread(new Runnable() {
  
              public void run() {
                  System.out.println("Inside thread");
  
                  //Int initialiser
                  new Thread(new Runnable() {
  
                      public void run() {
                          System.out.println("Setting integer value");
                          mytest.result.setIntValue(346635);
                          System.out.println("Integer value seted");
                          try {
                              Thread.sleep(1000);
                          } catch (InterruptedException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                      }
                  }).start();
  
                  //String initialiser
                  new Thread(new Runnable() {
  
                      public void run() {
                          System.out.println("Setting string value");
                          mytest.result.setStringValue("Hello hi");
                          System.out.println("String value seted");
                          try {
                              Thread.sleep(1000);
                          } catch (InterruptedException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                      }
                  }).start();
  
                  //Boolean initialiser
                  new Thread(new Runnable() {
  
                      public void run() {
                          System.out.println("Setting boolean value");
                          mytest.result.setBoolValue(true);
                          System.out.println("Boolean value seted");
                          try {
                              Thread.sleep(1000);
                          } catch (InterruptedException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                      }
                  }).start();
  
                  System.out.println("Thread is finished");
  
                  //Notify to main thread
                  synchronized (ThreadTest.waitObject) {
                      ThreadTest.boolValue = true;
                      ThreadTest.waitObject.notifyAll();
                  }               
              }
          }).start();
  
          try {
              synchronized (ThreadTest.waitObject) {
                  while (!ThreadTest.boolValue) {
                      ThreadTest.waitObject.wait();
                  }
              }
          } catch (InterruptedException ie) {
              ie.printStackTrace();
          }
  
          System.out.println("main finished");
          System.out.println("Result is : " + mytest.result.toString());
      }
  }
  </code></pre>
  
  <p><strong><em>Problem :-</em></strong></p>
  
  <p><em>My above code is not giving correct answer.  How can I do that?</em></p>
  
  <p><strong>Alternate solutions:</strong></p>
  
  <p>CountDownLatch class does the same. But I don't want to use that class. </p>
  
  <p>I looked <a href="https://stackoverflow.com/questions/7939257/wait-until-all-threads-finish-their-work-in-java">this similar solution</a> and I want to use methods of Thread only. </p>