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 Join() method in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Join() method in Java" Watch "Join() method in Java" New topic
Author

Join() method in Java

Matloob Hussain
Ranch Hand

Joined: May 13, 2011
Posts: 37

Hi,

Here is the code example, the output of two methods named method1() and method2() is different. The code difference in second method is thread T2 is started after join() method which is invoked on thread T1 but in first method, both threads T1 and T2 are started first and then join() method is invoked.

Could anyone please explain it..... thanks




Matloob
OCPJP 1.6
OCPWCD in progress.....
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

I am not very good in threads but as far as I understand it: (if I am wrong please somebody correct me)

t1.join() call here in method1 and method2 will block the main thread from becoming runnable till the thread t1 completes.

In method1 we start thread t2 before we call t1.join() so t1 and t2 both will be in runnable state and

In method2 we call t1.join() before we start thread t2, so as soon as we call t1.join() the main thread will be blocked.
Therefore t2 will now be started only after the t1 completes execution.


Piyush
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Suppose there are two threads T1 and T2 are running . and T1 starts T2 and now if you say T2.join() [in the T1 thread flow], then T1 blocks until T2 complete the job.
Suhrid Karthik
Ranch Hand

Joined: Aug 31, 2008
Posts: 58

In method1(), there is a possibility of both T1 and T2 running at the same time. In method2(), the main thread waits for T1 to complete by join()'ing itself to the end of T1. When T1 completes, the main thread resumes and starts T2.
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

Seetharaman Venkatasamy wrote:Suppose there are two threads T1 and T2 are running . and T1 starts T2 and now if you say T2.join() [in the T1 thread flow], then T1 blocks until T2 complete the job.


Yes you are right.

T2.join() will block the current executing thread. that is T1 if you call T2.join() from T1
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Join() method in Java