1

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?

Armaho
  • 37
  • 5
  • Why would `t1` be stopped? The two threads know nothing about each other. It looks like `t1` will always complete before `t2` due to the difference in sleep times. – quamrana Sep 14 '22 at 16:04
  • user253751's answer is right on the mark. Don't think of `t.join()` as "stopping" anything. Don't think of it as "pausing" anything. Picture it doing absolutely nothing at all, and picture it continuing to do nothing until thread `t` is finished. Then it returns. If you wanted to know how join() works "under the hood," we could talk about what it _actually_ does, but that discussion would have no bearing on how to use join() in a Python program. – Solomon Slow Sep 14 '22 at 16:19

3 Answers3

1

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

user253751
  • 57,427
  • 7
  • 48
  • 90
0

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.

0

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")
Veysel Olgun
  • 552
  • 1
  • 3
  • 15