File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Query about start() and join() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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: 1295

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)
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: 1295

You are welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query about start() and join()
 
Similar Threads
Threads Please help
Threads problem.
join() and no reference?
Join() method in Java
Salient join() characteristics