How to Create a Thread in Python

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

Program 1

import time
from  threading import *
def display():
    for i in range(1,11):
        print(i)
        time.sleep(1)
def show():
    for i in range(100,111):
        print(i)
        time.sleep(1)

# Calling (Main Thread)
T1=Thread(target=display)  # New Born
T2=Thread(target=show)     # New Born

T1.setName("First Thread")
T2.setName("Second Thread")

print(T1.getName())
print(T2.getName())

start=time.time()
T1.start()  # Runnable 
T2.start()  # Runnable 
T1.join()
T2.join()
end=time.time()
print(end-start)









# start=time.time()
# display()
# show()
# end=time.time()

# print(end-start)

 

Did you like our efforts? 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 *