.join() method delays a program’s flow of execution until the target thread has been completely read.
.join() method always returns
None. There is also a
timeout parameter that is set to
None by default. If set otherwise, it must be represented in seconds with a floating-point value.
The following example features two threads,
thread_B. Each thread makes a call to
.start(), immediately followed by a call to
import threadingdef is_divisible(dividend, divisor):print("Starting...")if(dividend % divisor == 0):print(True)else:print(False)print("Finished")thread_A = threading.Thread(target=is_divisible, args=(28, 14))thread_B = threading.Thread(target=is_divisible, args=(34, 7))thread_A.start()thread_A.join()thread_B.start()thread_B.join()
The output will look like this:
The second thread,
thread_B, cannot start before
thread_A is finished due to
In the example below, the
.join() method allows the last
print() statement to appear at the expected time in the program’s flow of execution: