This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

join() synchronizes the threads?

 
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I ran the below code 10 times and it prints both the threads synchronously. How is that possible? join is in the main function whose purpose is to wait for both threads to finish before quiting main.

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Eipe wrote:
I ran the below code 10 times and it prints both the threads synchronously. How is that possible? join is in the main function whose purpose is to wait for both threads to finish before quiting main.



Maybe I am tired or something and misunderstanding your statement, but when I run this program the threads are not running "synchronously" at all, quite the opposite. They are doing exactly what you have instructed them to do which is to run one after each other followed by the "main" thread. The output I get is the following



Your threads are going like this....main thread creates 2 new threads, the first thread t1 because of your start/join immediately runs its' run() ALL the way through. When t1 has finished its entire run() the main method starts again, and repeats the same thing with the t2 thread start/join and runs all the way through t2's run() without interruption. After this second interuption in main's execution main is runnable once again and completes the main().

 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad. Extremely sorry. What i mean to say is "shouldn't it be printing synchronously"?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know exactly what you mean by "it prints both the threads synchronously".

What join() does, is make the current thread wait until the thread you call it on stops. So in your code, you first start thread t1, and then you call t1.join(); in line 9, which makes the program wait until thread t1 has finished. Then you start thread t2 and do the same.

So the threads are not running in parallel, because you make the main thread wait for the first thread to finish before you start the second thread.

If you want both threads to run in parallel and then let the main thread wait for both threads to finish, you should re-order the statements. First start both threads, and then wait for them to finish:
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My bad. Extremely sorry. What i mean to say is "shouldn't it be printing synchronously"?



Ah, I see. Nope, if you refer back to the last portion of my last post it goes through the logic of the program in just a casual way. I think the part where you are getting confused is that you are thinking that BOTH your joins are being called prior to either thread accessing their run() method, where in reality you do not get to your t2.join until AFTER t1 has COMPLETELY finished its run().
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you guys! I got it.
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem! Nothing beats late night Java Ranching!
 
My pie came with a little toothpic holding up this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic