Armaho
Armaho

Reputation: 37

what does exactly the join() method do in python multithreading

I'm still learning how to use multithreading, and I'm confused about how the join() method works. based on what I read, the join() method stops everything until the thread terminates. but take a look at this code:

from threading import Thread

def do_something(i):
    print("Sleeping...")
    time.sleep(i)
    print(f"Done sleeping {i}")

start = time.perf_counter()

t1, t2 = Thread(target=do_something, args=(1,)), Thread(target=do_something, args=(10,))

t1.start()
t2.start()

t2.join()
print("t2 joined")
t1.join()
print("t1 joined")

And when we run this, it gave us the output below:

Sleeping...
Sleeping...
Done sleeping 1
Done sleeping 10
t2 joined
t1 joined

As you can see, I used t2.join() right after starting both threads, but the first thing that got printed is Done sleeping 1. But based on what I thought join() will do, I expected t1 to be stopped, and the program gave us the output below:

Sleeping...
Sleeping...
Done sleeping 10
Done sleeping 1
t2 joined
t1 joined

Can someone explain to me what I got wrong?

Upvotes: 1

Views: 623

Answers (3)

Veysel Olgun
Veysel Olgun

Reputation: 598

t.join() method will block the calling thread until thread t finished. I don't know how join() implemented, but probably it use semaphore mechanism. Please look at this also. Below myJoin() method will do same job mostly like join() method of Thread class did. Below program is only simple example to understand the background

from threading import Thread,Event
from time import sleep

class MyThread(Thread):

    def __init__(self):
        Thread.__init__(self)
        self.event=Event()

    def run(self):
        try:
            for i in range(5):
                print(i)
                sleep(1)
        finally:
            self.event.set()

    def myJoin(self,timeout=None):
        self.event.wait(timeout=timeout)

t = MyThread()
t.start()

print("waiting for thread t to finish")

t.myJoin()

print("thread t finished")

Upvotes: 0

Sparkling Marcel
Sparkling Marcel

Reputation: 968

When you join thread, it means every thread waits for the other to finish before terminating.

if you write

t2.join()
t1.join()

it means t2 and t1 are gonna wait for each other to finish before terminating (essentially "killing" the thread)

Essentially, it has to do with what we call deamon thread Initially, in python, once a thread finished it's job, it would terminate the whole program if it had nothing to do after.

The way to avoid that was to call .join() on every thread, which mean they would wait for every thread to finish before continuing (and potentially finishing the program)

Nowaday, thread are not deamon by default anymore (you can still make them deamon with a keyword argument).

So by default, once a thread has finished it's task it's gonna wait for every thread to finish before stopping.

Upvotes: 0

It waits for the thread to finish. That's all it does. Nothing more, and nothing less.

Upvotes: 1

Related Questions