• Post Reply Bookmark Topic Watch Topic
  • New Topic

join() synchronizes the threads?  RSS feed

 
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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • 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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!