ADVERTISEMENT
ADVERTISEMENT

Python Thread Class | join() Method with Example

Python Thread.join() Method: Here, we are going to learn about the join() method of Thread class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 07, 2020

Python Thread.join() Method

Thread.join() method is an inbuilt method of the Thread class of the threading module in Python. 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:

    from threading import Thread

Syntax:

    join(timeout=None)

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.

Example:

# 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)

Example:

# 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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.