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 usage 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 usage" Watch "join() method usage" New topic
Author

join() method usage

nikita mishra
Greenhorn

Joined: Feb 09, 2010
Posts: 12


This code makes the current thread (main method) to execute after the thread2 finishes its job. How to make thread2 to execute after thread1?









Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

you have to use thread2.join() inside thread1.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Harpreet Singh janda wrote:you have to use thread2.join() inside thread1.

Not exactly, if you want to start thread2 after thread1 completes, then you'll have to change the lines 34 and 35 i.e.
to
If you want thread2 to wait for thread1 to finish but main thread to complete without interruptions, then thread2 needs a reference to thread1 and call the join method on that reference...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ankit Garg wrote:
Harpreet Singh janda wrote:you have to use thread2.join() inside thread1.

Not exactly, if you want to start thread2 after thread1 completes, then you'll have to change the lines 34 and 35 i.e.
to
If you want thread2 to wait for thread1 to finish but main thread to complete without interruptions, then thread2 needs a reference to thread1 and call the join method on that reference...


See the coding :


I make it to Thread1 - first, Thread2 - second, Main - 3. But in some cases, I didn't get the whole Output of Thread1?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

But in some cases, I didn't get the whole Output of Thread1?

I don't know how you created and compiled the program, but for me thread1's output always completes before thread2 starts...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ankit Garg wrote:
But in some cases, I didn't get the whole Output of Thread1?

I don't know how you created and compiled the program, but for me thread1's output always completes before thread2 starts...


That's a mistake of mine.... Sorry....

Ankit wrote :

If you want thread2 to wait for thread1 to finish but main thread to complete without interruptions, then thread2 needs a reference to thread1 and call the join method on that reference...


Hi, You mean this.....

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Abimaran, I didn't exactly mean the same thing but its almost the same as your code. Your code is also going to stop the main method till thread1 completes, because you have a join call in the main method. Even if you remove that join call, the main method will wait for thread1 to complete because you called join in the constructor of Thread2 class. I was talking about this
Now the main method will complete without any waiting and thread2 will only start when thread1 is complete...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Ankit Garg!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: join() method usage