Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Thread join method question?

 
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • 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 ]
 
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 Spring Java
  • 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

 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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 Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
It will give me the powers of the gods. Not bad for a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic