I am confused uisng joins method in Threads Could somebody please explain I have read that the Parent Thread would wait for its child Thread , until the child completes its operation
I have a Parent Thread as 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 this , the output is
Hi
i m child
As per my understanding it should be in the reverse order
i m child
Hi
Please correct me if i am wrong
I have read that the Parent Thread would wait for its child Thread
No, not really,
This statement:
parent.join();
will block the current thread (the thread executing the join
) until parent
is finished.
In fact it won't affect the executing of parent
at all.
I'll clarify with an example:
class Test {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread() { public void run() {
System.out.println("t: going to sleep");
try { sleep(1000); } catch (InterruptedException e) { }
System.out.println("t: woke up...");
}};
System.out.println("main: Starting thread...");
t.start();
Thread.sleep(500);
System.out.println("main: sshhhh, t is sleeping...");
System.out.println("main: I'll wait for him to wake up..");
t.join();
System.out.println("main: Good morning t!");
}
}
Output (with timings to the left):
0 ms: main: Starting thread...
0 ms: t: going to sleep
500 ms: main: sshhhh, t is sleeping...
500 ms: main: I'll wait for him to wake up..
1000 ms: t: woke up...
1000 ms: main: Good morning t!
In your case you have three threads: main, parent and child. Main is the initial thread that is always created by the jvm to run your program. Parent and child are two threads that have been created in main. Your labeling of one thread as parent is a misnomer. It is the parent of no other threads. Rather it is the child of main. Join is intended so that one thread may wait for another to finish execution, before it continues.
A hypothetical example might be between a waiter and a chef. That is, a waiter cannot serve food until the chef has cooked it. So the waiter must needs to wait for (join) the chef to finish before serving the food.
public static void main(String[] args) {
Thread child = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("child doing its work");
}
});
child.start(); // start child thread
child.join(); // wait for child to finish
System.out.println("Now back in main. Child has finished its work");
}
You would get the expected behavior if you changed this:
parent.start(); //Starts the parent
child.start(); //Starts the child
parent.join(); //Waits for the parent to finish, which doesn't do much since it's probably already done anyway.
to this:
child.start(); //Starts the child running
child.join(); //Waits for it to finish, which will take a long time
parent.start(); //Starts the parent running
精彩评论