Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread join method question?

 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
long meng
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think that's because this code:Thread.currentThread().join();
this means currentThread must run after currentThread,so it will never end
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To no offense, a thread joining itself seems funny..

Don't take it seriously though, it was just a joke...
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic