Hi ranchers,
I would like you ask a question about
thread join method. I wrote a simple program but the join did not work and the program never stopped! I know that the join method took the currently running thread and join it at the end but in this case main method is never ending... What can be the problem.. I should see the System.out.println("Main Thread is stopped!"); in the console...
Here is the code
package com.sjcp.ThreadsExamples;
public class MyThread extends Thread {
public static void main(
String[] args) {
MyThread m = new MyThread();
m.setName("MyThread");
m.start();
//m.start();//IllegalThreadStateException
MyRunnable mr = new MyRunnable();
Thread m2 = new Thread(mr);
m2.setName("MyRunnable");
m2.start();
try
{
System.out.println("which thread will join >> " + Thread.currentThread().getName());
Thread.currentThread().join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main Thread is stopped!");
public void run()
{
try
{
System.out.println(Thread.currentThread().getName() + " is started!");
Thread.currentThread().sleep(7000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " is finished!");
}
package com.sjcp.ThreadsExamples;
public class MyRunnable implements Runnable {
public void run() {
System.out.println(Thread.currentThread().getName() + " is started!");
System.out.println(Thread.currentThread().getName() + " is finished!");
}
}
example output...
MyThread is started!
which thread will join >> main
MyRunnable is started!
MyRunnable is finished!
MyThread is finished!
Waiting your responses
[ October 18, 2008: Message edited by: Anut Walidera ]