Joining Threads in Python

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Program 1

# Program for Join in Mutithreading
import time
from threading import *
def display(n):
     print(current_thread().getName() +" Starts ")
     for i in range(1,n+1):
          print(current_thread().getName() , i)
          time.sleep(1)
     print(current_thread().getName() +" End ")

# def show():
#      print(current_thread().getName() +" Starts ")
#      for i in range(100,111):
#           print("show: ", i)
#           time.sleep(i)
#      print(current_thread().getName() +"End ")

#Calling Main Thread

print(current_thread().getName()+ " Starts ")
T1=Thread(target=display,args=(10,),name=" First Thread ")
T2=Thread(target=display,args=(10,),name=" Second Thread ")
T3=Thread(target=display,args=(20,),name=" Third Thread ")

T1.start()
T2.start()
T3.start()

for i in range(5):
     print("Main: ",i)
     time.sleep(1)


# print(T1.is_alive())
# print(T2.is_alive())

T1.join()
T2.join()
T3.join()

# print(T1.is_alive())
# print(T2.is_alive())
print(current_thread().getName()+ " Ends ***********")

 

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *