CodeGym /Java Course /Module 2. Java Core /join — waiting for a thread to end

join — waiting for a thread to end

Module 2. Java Core
Level 12 , Lesson 4
Available

"Hello, Amigo! I see you're making great strides in learning about threads."

"It wasn't so difficult after all."

That's great! Today you have an easy lesson, and the topic is the join method.

Imagine the following situation: the main thread has created a child thread to perform some task. Time passes, and now the main thread needs the results of the work performed by the child thread. But the child thread hasn't yet finished its work. What should the main thread do?

Good question. What should the main thread do?

"This is what the join method is for. It allows us to make one thread wait while another thread finishes its work:"

Code Description
class Printer implements Runnable
{
private String name;
public Printer(String name)
{
this.name = name;
}
public void run()
{
System.out.println("I’m " + this.name);
}
}
Class that implements the Runnable interface.
public static void main(String[] args)
{
Printer printer1 = new Printer("Nick");
Thread thread1 = new Thread(printer1);
thread1.start();

thread1.join();
}
The main thread creates a child thread – thread1.

Then it starts it by calling thread1.start();

And then it waits for it to complete – thread1.join();

One thread can call the join method on a second thread's Thread object. As a result, the first thread (which called the method) stops its work until the second thread (whose object's join method was called) is finished.

We need to distinguish between two things here: we have a thread (a separate execution environment) and we have a Thread object.

"That's it?"

"Yes."

"But why do we have to create a thread and then immediately wait for it to complete?"

"It might not need to right away. It could be after some time has passed. After starting its first child thread, the main thread can assign many more tasks to other threads (by creating them and calling the start method). Then when it doesn't have any work left, it needs to process the results of the first child thread. Whenever you need to wait for another thread to finish working, you need to call the join method."

"Got it."

Comments (19)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Hoist Level 35, San Diego, United States
13 November 2022
See this share below >> Krunoslav Krainović >> --- YouTube videos on Multi Threading
Thành Black Level 49, Hanoi
26 September 2021
why it is not end().
Artur Level 22, Moorends, United Kingdom
15 November 2020
i kept seeing people talking about how many people finished the lesson but while trying to find it now i cant.. where thats shown? :P
Andrei Level 41
11 December 2020
I believe they are not referring to how many people finished the lesson but how many people finished a specific task. This appears after you successfully complete and check a task. A pop up appears which displays the information I believe you are referring to. :)
Artur Level 22, Moorends, United Kingdom
12 December 2020
yea but i could not see it and now i know why.. it is only visible if you do the tasks in intellij not on the website :)
Andrei Level 41
14 December 2020
are you sure? i remember they were displayed on the website too when you correctly verified a task. maybe they changed that.
Artur Level 22, Moorends, United Kingdom
15 December 2020
yea i remember something as well and was wondering if its just me but they must have changed because i havent seen it on web for a long time now
Lawson Level 29, Lagos, Nigeria
30 August 2020
So the join method is used to wait for threads ... Issorite
Jen P Level 26
3 August 2019
Not sure I understand this : "One thread can call the join method on a second thread's Thread object. As a result, the first thread (which called the method) stops its work until the second thread (whose object's join method was called) is finished." I check java 8 official doc , the parameter of join method are not allowed as thread object, then how to "call the join method on a second thread's Thread object" ?
Tara Rosenthal Level 18, Farmers Branch, United States
29 April 2020
You don't use the thread object as a parameter. The thread object in this example is stored in thread1, so calling thread1's join method is done like this: thread1.join(). In this example, the main method is where thread1.join is called, so that means that the main thread is doing the calling.
Henk Level 19, Pretoria, South-Africa
23 May 2019
why not call it waitForThread("thread1") ? Why do language creators always go the most illogical or difficult way (syntax wise) ? LOL!
Daniel Walbolt Level 22, Waterville, United States
4 July 2020
Just wait until you get into using APIs/Libraries/Frameworks developed by other companies. They make names that are hardly understandable unless you wrote the code. Every once in awhile, you see developers who name it properly, but otherwise, it's mostly for conciseness instead of precision. On the other hand, join, accurately and concisely describes what the method does. It essentially combines two threads, but not in the way you expect. It makes their operation paths join together, but not the work they're doing. In other words, the main thread makes plans to have lunch together with the child thread, and the child thread responds by saying "after work." So the main method waits so they can share the same food together!
Andrei Level 41
11 December 2020
Great example!
Samuel Level 16, London, United Kingdom
1 July 2021
Practical example. Thanks!
Jonaskinny Level 25, Redondo Beach, United States
10 March 2022
I hear you in general, but in this case ( since its the implicit main thread running tread1.join() ) think of the main thread as a listener (observer pattern) registering itself with thread1.
Roy Level 22, Bangkok, Thailand
3 May 2019
Hopefully one day i understand the WHY, but mostly WHEN to use threads in an application =)
Krunoslav Krainović Level 16, Croatia
16 November 2019
thats why you need to use youtube,codegym CANT be your only source for example when you use amazon app and you type something in search bar you send request to server and if its same thread your app would freeze,thats why when you click search you create new thread and that thread is used to get request from server to app so your app wont freeze. https://www.youtube.com/watch?v=L95658yXRgI https://www.youtube.com/watch?v=Xj1uYKa8rIw and i would suggest you to watch whole collage class in java from Stanford to understand better programming methodology https://www.youtube.com/watch?v=KkMDCCdjyW8&list=PL84A56BC7F4A1F852 Programming Methodology (Stanford)
Ifeanyi Level 31, Lagos, Nigeria
23 January 2020
Thank you for this
Fadi Alsaidi Level 34, Carrollton, TX, USA
9 February 2020
I second Ifeanyi in my gratitude. everyone who read the comments should watch those videos for better clarity especially around the join method of threads. Here is my understanding of join: when join is called on any thread, it will cause the main thread to pause from executing any lines in main() until that particular thread is done executing. off course that doesn't mean that the other threads will pause. Only main thread will pause while the other threads will continue to work until they are finished. This can have a major impact on lines below the threads that are asked to join(), and major impact on loops who would pause from repeating the same line/lines until the line/lines that involve/involves waiting for a thread to be done.