• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Thread join method question?

 
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Have you no shame? Have you no decency? Have you no tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic