Python Thread join() Method with Example

Python Thread.join() Method: In this tutorial, we will learn about the join() method of Thread class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 24, 2023

Python Thread.join() Method

The Thread.join() method is an inbuilt method of the Thread class of the threading module. Whenever this method is called for any Thread object, it blocks the calling thread till the time the thread whose join() method is called terminates, either normally or through an unhandled exception.

Module

The following module is required to use join() method:

import threading

Class

The following class is required to use join() method:

from threading import Thread

Syntax

The following is the syntax of join() method:

join(timeout=None)

Parameter(s)

The following are the parameter(s):

  • timeout: It is an optional parameter, which specifies a timeout for the operation in seconds. it should be a floating-point number. When a timeout argument is missing, the operation will block until the thread terminates.

Return Value

The return type of this method is <class 'NoneType'>, it returns nothing.

Thread.join() Method Example 1

# Python program to explain the
# use of join() method in Thread class

import time
import threading

def thread_1(i):
    time.sleep(2)
    print('Value by Thread 1:', i)

def thread_2(i):
    time.sleep(5)
    print('Value by Thread 2:', i)

def thread_3(i):
    print('Value by Thread 3:', i)    

    
# Creating three sample threads 
thread1 = threading.Thread(target=thread_1, args=(1,))
thread2 = threading.Thread(target=thread_2, args=(2,))
thread3 = threading.Thread(target=thread_3, args=(3,))

# Running three thread object
thread1.start()
thread1.join()
thread2.start()
thread2.join()
thread3.start()
thread3.join()

print()
# Creating another 3 threads
thread4 = threading.Thread(target=thread_1, args=(1,))
thread5 = threading.Thread(target=thread_2, args=(2,))
thread6 = threading.Thread(target=thread_3, args=(3,))

thread4.start()
thread5.start()
thread6.start()
thread4.join()
thread5.join()
thread6.join()

Output

Value by Thread 1: 1
Value by Thread 2: 2
Value by Thread 3: 3

Value by Thread 3: 3
Value by Thread 1: 1
Value by Thread 2: 2

In the initial three Thread objects, we first created a thread and waited for it to execute and then joined to the main thread. So they are printed in the same order as called.

In the next three Thread objects, they are running simultaneously, so they get printed according to the time they should be executed,

time(thread3)<time(thread2)<time(thread1)

Thread join() Method Example 2

# Python program to explain the
# use of join() method in Thread class 
# with timeout parameter defined

import time
import threading

def thread_1(i):
    time.sleep(2)
    print('Value by Thread 1:', i)

def thread_2(i):
    time.sleep(5)
    print('Value by Thread 2:', i)
    
# Creating three sample threads 
thread1 = threading.Thread(target=thread_1, args=(1,))
thread2 = threading.Thread(target=thread_2, args=(2,))

# Running three thread object
thread1.start()
thread1.join(timeout=5)
thread2.start()
thread2.join()

Output

Value by Thread 1: 1
Value by Thread 2: 2

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.