java - Query in Threads Join -


i confused uisng joins method in threads please explain have read parent thread wait child thread , until child completes operation

i have parent thread shown :

public class join implements runnable {      public void run() {          system.out.println("hi");      }      public static void main(string[] args) throws exception {         join j1 = new join();         thread parent = new thread(j1);          child c = new child();          thread child = new thread(c);          parent.start();         child.start();         parent.join();      } } 

child thread :

    public class child implements runnable {               public void run() {                     try {                         thread.currentthread().sleep(100000);                     } catch (interruptedexception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                             system.out.println("i m child");              }   } 

upon executing , output is

hi

i m child

as per understanding should in reverse order

i m child

hi

please correct me if wrong

in case have 3 threads: main, parent , child. main initial thread created jvm run program. parent , child 2 threads have been created in main. labeling of 1 thread parent misnomer. parent of no other threads. rather child of main. join intended 1 thread may wait finish execution, before continues.

a hypothetical example might between waiter , chef. is, waiter cannot serve food until chef has cooked it. waiter must needs wait (join) chef finish before serving food.

public static void main(string[] args) {      thread child = new thread(new runnable() {         @override         public void run() {             system.out.println("child doing work");         }     });      child.start(); // start child thread     child.join(); // wait child finish      system.out.println("now in main. child has finished work"); } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -