Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help understanding join()

 
Udo Klein
Greenhorn
Posts: 2
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

this is my first post/question, so please bear with me - and apologies in advance for the length of the post.

I'm trying to understand the behavior of join() using the following code.

What I expected was that when the main thread executes the try block and calls the method join() on T1, it (the main thread) stops and waits for T1 to finish executing before becoming runnable again. However, the following result suggests that this is not the case, since the main thread is running again after the counter of thread T1 is at 83, and continues counting down from 188!


Can anybody please explain why thread T1 is not counting down to 1, but stops at 83? Shouldn't the main thread wait until thread T1 completes?

Thanks and best wishes,
Udo
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Udo Klein wrote:
Can anybody please explain why thread T1 is not counting down to 1, but stops at 83? Shouldn't the main thread wait until thread T1 completes?


Yeah, the documentation does imply that the join() method is to wait until the thread has completed (has died) doesn't it? It reality, if you read the later paragraphs of the javadoc, it actually waits until the thread is not alive (by the isAlive() method). And since you never started the thread (that is represented by the object referred to by T1), it is not an alive thread, and hence, the join() method returns immediately.


And BTW, welcome to the ranch. I have also added this topic to the threads forum for you.

Henry
 
Paweł Baczyński
Bartender
Posts: 1876
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is you are invoking join() on a Thread instance that has never started.
You have overridden start() method of the Thread class. Inside this method you create another Thread instance and start() that instance, not your original Thread.

What was the reason of this strange strategy?
If you wanted to pass names to your threads why not call super(threadName) in a constructor of your ThreadDemo class?
Or call setName().

And welcome to the Ranch!
 
Udo Klein
Greenhorn
Posts: 2
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick replies. The code mislead me into thinking that t1 actually gets started by calling start(), but that was not the case.

@Pawel: there was no reason for this strange strategy, I just used some code I found in a tutorial.

I've now changed the code, and it behaves as expected. Now thread t1 actually gets started, and after the main thread calls join() on thread t1, the main thread waits for t1 to complete.



A sample result:
 
Paweł Baczyński
Bartender
Posts: 1876
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For an excellent first post, have a cow!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic