Python Thread Synchronization in Multithreading

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

Program 1

import time
from  threading import *
l=Lock()
def printable(n):
    l.acquire()
    for i in range(1,11):
        print(n*i)
        time.sleep(1)
    l.release()  

# Calling (Main Thread)
T1=Thread(target=printable,args=(5,),name="First")
T2=Thread(target=printable,args=(7,),name="Second")
T3=Thread(target=printable,args=(9,),name="Third")
start=time.time()
T1.start()
T2.start()
T3.start()
T1.join()
T2.join()
T3.join()
end=time.time()
print("Total time",(end-start))

 

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback 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 *