Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query about start() and join()

 
gunjan khanuja
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that join will ask the current running thread to wait for some time till the thread on which join is invoked gets completed .but if join is invoked on the same thread which was started earlier.then what happens?can anybody please explain that??
Here is the code snippet

public class ProjectNew implements Runnable {
public static void main(String[] args) throws Exception {
Projectnew n=new Projectnew();
Thread t = new Thread(n);
t.start();
System.out.print("Test 1");
t.join();
System.out.print("Test2");
}

public void run() {
System.out.println("Test 3");
}
}
Please explain how this code works??what actually happens when start and join is called??
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code, you are starting a thread, and then calling join on it.

When you start thread t, there are actually two threads - main thread and t. Now, you are calling t.join() inside main thread. Here, main thread goes in waiting state till t thread finishes. Once t finishes, main will continue.

I hope this helps.
 
gunjan khanuja
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes ..thanks..now i hav understood.Problem is resolved.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic