Learn

In the previous exercise, we started working with multiple threads and noticed some interesting behavior. We will investigate this behavior more with the .join() threading method.

We can use .join() to tell one thread to wait for this thread to stop before moving on. Let’s see how we implement this and then try it out in our previous example.

We use .join() after each thread has already been initiated.

Click here if you need a reminder of what the first approach looked like.
# create each thread t1 = threading.Thread(target=target_function(), args=(arg1,)) t2 = threading.Thread(target=target_function(), args=(arg2,)) t3 = threading.Thread(target=target_function(), args=(arg3,)) # start each thread t1.start() t2.start() t3.start()

With the first approach from the previous exercise, we do:

t1.join() t2.join() t3.join()
Click here if you need a reminder of what the second approach looked like.
threads = [] # list of arguments to use args = [arg1, arg2, arg3] # iterate through the length of arguments for i in range(len(args)): # create thread t = threading.Thread(target=target_function, args=(arg[i],) # add thread to threads list threads.append(t) # start thread t.start()

With the second approach from the previous exercise, we do:

for t in threads: t.join()

The second approach is much easier for keeping track of our threads and is much more efficient if we have numerous.

Instructions

1.

In script.py, the code loaded in is almost identical to the final code you had in the previous exercise, but an empty list called threads has been added to main_threading().

Within the for loop, beneath # add append code here, write a line of code so that t is added to list threads.

Inside the terminal, type python3 script.py and press Enter. To check your answer, click Check Work once your program finishes running.

2.

Underneath # add join code here, write a for loop that traverses threads and call .join() for each thread in threads.

Inside the terminal, type python3 script.py and press Enter. To check your answer, click Check Work once your program finishes running.

Is your output different than the output of the previous exercise?

3.

You should see something similar to the following output:

Codecademy Chelsea Hisham Ashley says hello! says hello! says hello! says hello! Threading Elapsed Time: 2.002612964999571 seconds

Let’s walk through this. Now that we are using .join(), main_threading() does not complete until each thread has been executed. Therefore, we get a more accurate measurement of two seconds. If we were to run this with sequential programming, it would take eight seconds since calling:

greeting_with_sleep('Codecademy') greeting_with_sleep('Chelsea') greeting_with_sleep('Hisham') greeting_with_sleep('Ashley')

would run into time.sleep(2) four times.

You may also wonder why the output order is the names, and then the greeting “says hello!”. This is because when each thread is blocked by time.sleep(2) they begin to work concurrently to complete each task. In the next checkpoint, we’ll see some interesting behavior resulting from this concurrency.

Click Check Work to move onto the next task.

4.

Change the following print statement:

print("says hello!")

to:

print(string + " says hello!")

Before running script.py, make a prediction about what the order of the outputs will be?

Inside the terminal, type python3 script.py and press Enter. To check your answer, click Check Work once your program finishes running.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?