World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Download our e-book of Introduction To Python
4.5 (1,292 Ratings)
559 Learners
Neha Kumawat
a year ago
thread.start_new_thread ( function, args[, kwargs] )
import _thread as th
import time
#Defining a function for the thread
def print_time( threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print ("%s: %s" % (threadName, time.ctime(time.time()) ))
#Creating two threads as follows
try:
th.start_new_thread(
print_time, ("Thread-1", 2, ) )
th.start_new_thread(
print_time, ("Thread-2", 4, ) )
except:
print("Error occured : unable to start thread, please try again")
while True:
pass
Thread-1: Sat Jan 1 11:35:31 2022
Thread-2: Sat Jan 1 11:35:33 2022
Thread-1: Sat Jan 1 11:35:33 2022
Thread-1: Sat Jan 1 11:35:35 2022
Thread-2: Sat Jan 1 11:35:37 2022
Thread-1: Sat Jan 1 11:35:37 2022
Thread-1: Sat Jan 1 11:35:39 2022
Thread-2: Sat Jan 1 11:35:41 2022
Thread-2: Sat Jan 1 11:35:45 2022
Thread-2: Sat Jan 1 11:35:49 2022
import threading
import time
exitFlag = 0
class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print ("Starting " + self.name)
print_time(self.name, 5, self.counter)
print ("Exiting " + self.name)
def print_time(threadName, counter, delay):
while counter:
if exitFlag:
threadName.exit()
time.sleep(delay)
print ("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
# Create new threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
# Start new Threads
thread1.start()
thread2.start()
print ("Exiting Main Thread")
Starting Thread-1
Starting Thread-2
Exiting Main Thread
Thread-1: Sat Jan 1 11:56:43 2022
Thread-1: Sat Jan 1 11:56:44 2022
Thread-2: Sat Jan 1 11:56:44 2022
Thread-1: Sat Jan 1 11:56:45 2022
Thread-1: Sat Jan 1 11:56:46 2022
Thread-2: Sat Jan 1 11:56:46 2022
Thread-1: Sat Jan 1 11:56:47 2022
Exiting Thread-1
Thread-2: Sat Jan 1 11:56:48 2022
Thread-2: Sat Jan 1 11:56:50 2022
Thread-2: Sat Jan 1 11:56:52 2022
Exiting Thread-2
import threading
import time
class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print ("Starting " + self.name)
# Get lock to synchronize threads
threadLock.acquire()
print_time(self.name, self.counter, 3)
# Free lock to release next thread
threadLock.release()
def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print ("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
threadLock = threading.Lock()
threads = []
# Create new threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
# Start new Threads
thread1.start()
thread2.start()
# Add threads to thread list
threads.append(thread1)
threads.append(thread2)
# Wait for all threads to complete
for t in threads:
t.join()
print ("Exiting Main Thread")
Starting Thread-1
Starting Thread-2
Thread-1: Sat Jan 1 11:58:12 2022
Thread-1: Sat Jan 1 11:58:13 2022
Thread-1: Sat Jan 1 11:58:14 2022
Thread-2: Sat Jan 1 11:58:16 2022
Thread-2: Sat Jan 1 11:58:18 2022
Thread-2: Sat Jan 1 11:58:20 2022
Exiting Main Thread
import queue
import threading
import time
exitFlag = 0
class myThread (threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print ("Starting " + self.name)
process_data(self.name, self.q)
print ("Exiting " + self.name)
def process_data(threadName, q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data = q.get()
queueLock.release()
print ("%s processing %s" % (threadName, data))
else:
queueLock.release()
time.sleep(1)
threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
queueLock = threading.Lock()
workQueue = queue.Queue(10)
threads = []
threadID = 1
# Create new threads
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
# Fill the queue
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
# Wait for queue to empty
while not workQueue.empty():
pass
# Notify threads it's time to exit
exitFlag = 1
# Wait for all threads to complete
for t in threads:
t.join()
print ("Exiting Main Thread")
Starting Thread-1
Starting Thread-2
Starting Thread-3
Thread-3 processing One
Thread-1 processing Two
Thread-2 processing Three
Thread-3 processing Four
Thread-1 processing Five
Exiting Thread-2
Exiting Thread-3
Exiting Thread-1
Exiting Main Thread