wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Query about start() and join() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Query about start() and join()" Watch "Query about start() and join()" New topic
Author

Query about start() and join()

gunjan khanuja
Ranch Hand

Joined: Apr 16, 2012
Posts: 37
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

Joined: Dec 08, 2010
Posts: 1509
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
gunjan khanuja
Ranch Hand

Joined: Apr 16, 2012
Posts: 37
Yes ..thanks..now i hav understood.Problem is resolved.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

You are welcome.
 
Consider Paul's rocket mass heater.
 
subject: Query about start() and join()