• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of the join method  RSS feed

 
Douglas Kent
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:
public class myThread extends Thread
{
String name;
public myThread (String theName)
{
name = theName;
}
public void run()
{
try
{
Thread.sleep(5000);
}
catch (InterruptedException ex) {}
System.out.println( "I'm " + name );
}
public static void main (String args[])
{
myThread T1 = new myThread( "Fred" );
T1.start();
System.out.println( "I'm the main" );
}
}
Is there any way to ensure the main message will show up after the thread message? Other than using "sleep" in the main?
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can use the join() method and its various overloaded methods to help you out. I have modified your program to incorporate the feature. Basically join will wait for a thread to die before continuing the work of the thread of origin ie the main thread.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!