aspose file tools
The moose likes Beginning Java and the fly likes join() synchronizes the threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "join() synchronizes the threads?" Watch "join() synchronizes the threads?" New topic
Author

join() synchronizes the threads?

John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 205
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.


www.cs-repository.info
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
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().


Everyday in every way, we get a little better.
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 205
My bad. Extremely sorry. What i mean to say is "shouldn't it be printing synchronously"?
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12921
    
    3

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
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

Joined: May 23, 2008
Posts: 205
Thanks you guys! I got it.
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
No problem! Nothing beats late night Java Ranching!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: join() synchronizes the threads?
 
Similar Threads
join() method
Confused in Thread join() code
join() method in Thread doesn't work as expected
Problem in understanding join()
deadlock?