Demonstrate Join() In Java Thread
Chapter:
Thread
Last Updated:
13-08-2016 13:10:40 UTC
Program:
/* ............... START ............... */
class MyThreadJoinDemo implements Runnable {
int count;
MyThreadJoinDemo() {
count = 0;
}
public void run() {
System.out.println("MyThread starting.");
try {
do {
Thread.sleep(500);
System.out.println("In MyThread, count is " + count);
count++;
} while (count < 6);
} catch (InterruptedException exc) {
System.out.println("MyThread interrupted.");
}
System.out.println("MyThread terminating.");
}
}
public class JavaDemonstrateJoin {
public static void main(String args[]) {
System.out.println("Main thread starting.");
Thread thrd = new Thread(new MyThreadJoinDemo());
thrd.start();
try {
thrd.join();
} catch (InterruptedException exc) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread ending.");
}
}
/* ............... END ............... */
Output
Main thread starting.
MyThread starting.
In MyThread, count is 0
In MyThread, count is 1
In MyThread, count is 2
In MyThread, count is 3
In MyThread, count is 4
In MyThread, count is 5
MyThread terminating.
Main thread ending.
Tags
Demonstrate Join(), Java, Thread