wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread join method question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread join method question?" Watch "Thread join method question?" New topic
Author

Thread join method question?

Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

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 ]

blog: http://tunatore.wordpress.com
SCJP 6.0 + SCWCD 1.5
long meng
Ranch Hand

Joined: Oct 10, 2008
Posts: 58
i think that's because this code:Thread.currentThread().join();
this means currentThread must run after currentThread,so it will never end


SCJP 5.0 98%<br />SCWCD 5.0 in progress . . .
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

Yes I got it you are right!!!

It is really interesting, I learned that you should start a join method on started thread rather than saying current thread If you say current thread it waits forever....

Thank you for your response

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

To no offense, a thread joining itself seems funny..

Don't take it seriously though, it was just a joke...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

 
 
subject: Thread join method question?
 
Similar Threads
Lock Mechanism Doubt
Please explain this code
how to use join method so that thread1 should wait for thread2?
Thread class join() method
Concurency logic