aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes join() method in Thread doesn't work as expected 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 Thread doesn Watch "join() method in Thread doesn New topic
Author

join() method in Thread doesn't work as expected

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Couldn't find an example in Sierra/Bates that uses join(), so I created my own (based on code from other parts of Sierra/Bates)

The intent of my code is for main thread to execute THEN the thread with nr job. However, the thread with nr job executes, then main:




Below is the output:

Run by Thread-0, x is 1
Run by Thread-0, x is 2
Run by Thread-0, x is 3
Run by Thread-0, x is 4
Run by Thread-0, x is 5
Run by main, x is 1
Run by main, x is 2
Run by main, x is 3
Run by main, x is 4
Run by main, x is 5



I was expecting main to execute first. Please clarify.


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

You are understanding the meaning of join method opposite. When A method calls b.join() it means that A will resume execution after thread b completes...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Sandra Bachan wrote:
I was expecting main to execute first. Please clarify.

you have already started thread t before calling
if you want main thread to complete first and then thread t invokes
use
statement after t.join(); statement not before.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
I'm trying to rearrange code so I can analyze the difference if I use join() vs not using join(). However, I get the same output regardless:




Output is always the same, whether I include t2.join() or not:

Run by t1, x is 1
Run by t2, x is 1
Run by t1, x is 2
Run by t2, x is 2
Run by t1, x is 3
Run by t2, x is 3
Run by t1, x is 4
Run by t2, x is 4
Run by t1, x is 5
Run by t2, x is 5


I would think that the output would be t1, followed by execution of all t2, then t1 resumes.

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Sandra Bachan wrote:
Output is always the same, whether I include t2.join() or not.

if you are not using

output can not be determined, as nothing is guaranteed in this case,if you are not using join() mehtod.
JVM can invoke any thread t1 or t2 ,nobody knows,depends upon the thread scheduler of your OS.
every time whether you will get same or different output,no one knows, it cannot be determined which thread will invoked first.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
By the way, I am running my code on Ubuntu 9.04 which is on a virtual machine on Mac OS. However, when I run the following code on another machine that is Ubuntu 10.04 which is on a virtual machine on Ubuntu 9.04, I do notice slight difference.

Please note the modified code:



The abridged output:

Run by t1, x is 1
Run by t2, x is 1
Run by t1, x is 2
Run by t2, x is 2
Run by t1, x is 3
Run by t2, x is 3
Run by t1, x is 4
Run by t2, x is 4
Run by t1, x is 5
Run by t2, x is 5
Run by t2, x is 6

Run by t1, x is 6
...
Run by t1, x is 48
Run by t2, x is 48
Run by t1, x is 49
Run by t2, x is 49
Run by t1, x is 50
Run by t2, x is 50



It seems that t2 executes before t1, but at the very end t2 is the last to execute.

I I remove t2.join(), I get the following abridged output

Run by t1, x is 1
Run by t2, x is 1
Run by t1, x is 2
Run by t2, x is 2
Run by t2, x is 3

Run by t1, x is 3
Run by t1, x is 4

Run by t2, x is 4
Run by t2, x is 5

Run by t1, x is 5
Run by t2, x is 6
Run by t1, x is 6
...
Run by t2, x is 48
Run by t1, x is 48
Run by t2, x is 49
Run by t1, x is 49
Run by t2, x is 50
Run by t1, x is 50



I still don't have a firm grasp of this. Can you refer to any code that explains join() method.
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

modify your code at line25

also use
before t1.join();
try this,and see the output.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Arjun Srivastava wrote:modify your code at line25

also use
before t1.join();
try this,and see the output.


@ Arjun: I did as you advised; below is the modified code:



However, t1 executes and then t2 executes.

If I comment out the t1.join() method, then t1 and t2 are interleaved.

Please tell me if I understand correctly: When we say t1.join(), it gives t1 priority over t2 such that t2 must wait for t1 to finish execution before t2 begins?
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Sandra Bachan wrote:
If I comment out the t1.join() method, then t1 and t2 are interleaved.
Please tell me if I understand correctly: When we say t1.join(), it gives t1 priority over t2 such that t2 must wait for t1 to finish execution before t2 begins?


this statement simply means that t2 will join to the end of t1 or you can say wait until t1 is done.
yeah you got it right.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

There is no point of calling join() on threads, which haven't started yet. The implementation is to "wait until the thread is no longer alive". If the thread is running, obviously, it means to wait til the thread is finished.

But since it only uses the alive flag to determine that it is finished, calling join() on a thread object that hasn't been started, will return immediately... as threads that haven't been started are not alive.

The calling thread waits until the thread finish it's job!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
I was studying this some more, and I changed the code such that t1 and t2 starts, then t1.join() is called. This is different from my previous code where t1 starts, t1.join() is called and then t2 starts.

The output I expected was the execution of all t1's followed by all t2's. However, t1 and t2 is interleaved. Please explain what is going on.


New code:



Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
You have three threads running: The main thread, t1 and t2.

You are calling t.join() from within the main thread so the main thread joins t1. The behaviour of t1 and t2 can't be predicted, but "interleaved" execution is fine.

If you want a thread to join another thread then you have to call join from inside the first thread.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Hauke Ingmar Schmidt wrote:You have three threads running: The main thread, t1 and t2.

You are calling t.join() from within the main thread so the main thread joins t1. The behaviour of t1 and t2 can't be predicted, but "interleaved" execution is fine.

If you want a thread to join another thread then you have to call join from inside the first thread.


After reading this and looking at the code again, it now makes sense!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: join() method in Thread doesn't work as expected