Start New Discussion within our Software Development Community

Actually when the main function is running in a class, it implictly speaks a Main thread is in running in background.

public static void main(String[] args){
   ......    
}    

Now when we create a thread objects in the above function like this . For
example :

 public class NewThread extends Thread {
         Thread t;               
          public NewThread(String name){
              this.t = new Thread(this,name);
              this.t.start();
          }

          public static void main(String[] args){
              NewThread obj1 = new NewThread(); // 1
              NewThread obj2 = new NewThread(); // 2
         }
      }

Q) When obj1 and obj2 is created, means child threads are created under main thread(i.e parent thread.) isn't it ?. if the answer to this question is yes then

Q) when i call join() methods on each of the child object, then main thread will wait until the child thread is exited isn't it? or is there anything else. For Example

    public class NewThread extends Thread {          
          Thread t;               
          public NewThread(String name){
              this.t = new Thread(this,name);
              this.t.start();
          }

           public static void main(String[] args){
               NewThread obj1 = new NewThread(); // 1
               NewThread obj2 = new NewThread(); // 2

               try{
                 obj1.join();
                 obj2.join();
                }catch(InterruptedException e){
                   System.out.println(e);
                }
                System.out.println("Main thread is exited");

           }
     }

 So after displaying the line "Main thread is exited.",no other child thread should be running.isn't it?

After a very quick look at that code I see 5 threads:
The main thread
obj1
obj1.t
obj2
obj2.t

of which main, obj1.t and ob2.t are started
but you try to join obj1 and obj2, which have not been started

This article has been dead for over six months. Start a new discussion instead.