• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Need help understanding join()

 
Greenhorn
Posts: 2
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For an excellent first post, have a cow!
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic