In Python code: Put the thread_function on a thread. Put 3 threads into a queue and run the threads. import queue queue = queue.Queue def thread_function(name): print("Thread %s: starting", name) time.sleep(2) print("Thread %s: finishing", name) # start threads by passing function to Thread constructor from pprint import pprint import threading import time def threadfunc(*t): print(">>>>",*t) time.sleep(1) print('[',*t,']') time.sleep(2) print("<<<<",*t) arg1 = ("AAAAAAAA") threadA = threading.Thread(target=threadfunc,args=arg1) threadA.start() arg2 = ("BBBBBBBB") threadB = threading.Thread(target=threadfunc,args=arg2) threadB.start() arg3 = ("CCCCCCCC") threadC = threading.Thread(target=threadfunc,args=arg3) threadC.start() threadA.join() threadB.join() threadC.join() # multiple threads import threading import time tnames = ('AAAAAAAA','BBBBBBBB','CCCCCCCC') count = len(tnames) threadlist = [] count = 3 def threadfunc(*t): print(">>>>",*t) time.sleep(1) print('[',*t,']') time.sleep(2) print("<<<<",*t) def threadList(): for index in range(count): targ = (tnames[index]) thread = threading.Thread(target=threadfunc,args=targ) print("inserting "+targ) threadlist.append(thread) for index in range(count): print("starting "+tnames[index]) threadlist[index].start() for index in range(count): print("joining "+tnames[index]) threadlist[index].join() threadList() # thread inheritance import threading import time tnames = ('AAAAAAAA','BBBBBBBB','CCCCCCCC') count = len(tnames) threadlist = [] count = 3 def threadfunc(*t): print(">>>>",*t) time.sleep(1) print('[',*t,']') time.sleep(2) print("<<<<",*t) class TimeThread (threading.Thread): def __init__(self, threadID, name): threading.Thread.__init__(self) self.threadID = threadID self.name = name def run(self): threadfunc(self.name) for index in range(count): print("inserting "+tnames[index]) threadlist.append(TimeThread(index+1, tnames[index])) # Start new Threads for index in range(count): print("starting "+threadlist[index].name) threadlist[index].start() # Join Threads for index in range(count): print("joining "+threadlist[index].name) threadlist[index].join() print("Exiting Main Thread") # thread queue import queue import threading import time exitFlag = False def threadfunc(threadName, que): print('>>>>'+threadName) time.sleep(1) while not exitFlag: if not workQueue.empty(): data = que.get() print('[',*data,']') time.sleep(2) print('<<<<'+threadName) class TimeThread (threading.Thread): def __init__(self, name, q): threading.Thread.__init__(self) self.name = name self.que = q def run(self): print("Starting " + self.name) threadfunc(self.name, self.que) print("Exiting " + self.name) threadList = ["AAAAAAAA", "BBBBBBBB", "CCCCCCCC"] nameList = ["Alpha", "Beta", "Gamma"] workQueue = queue.Queue(len(nameList)) threads = [] # Create new threads for tName in threadList: thread = TimeThread(tName, workQueue) thread.start() threads.append(thread) # Fill the queue for word in nameList: print("inserting word "+word+" into queue") workQueue.put(word) # Wait for queue to empty while not workQueue.empty(): pass # Notify threads it's time to exit exitFlag = True # Wait for all threads to complete for thread in threads: thread.join() print("Exiting Main Thread") # queue lock import queue import threading import time exitFlag = False threadList = ["AAAAAAAA", "BBBBBBBB", "CCCCCCCC"] nameList = ["Alpha", "Beta", "Gamma"] queueLock = threading.Lock() workQueue = queue.Queue(3) threads = [] def threadfunc(threadName, q): print('>>>>'+threadName) time.sleep(1) while not exitFlag: if not workQueue.empty(): queueLock.acquire() data = q.get() print('[',*data,']') queueLock.release() time.sleep(2) print('<<<<'+threadName) class TimeThread (threading.Thread): def __init__(self, name, q): threading.Thread.__init__(self) self.name = name self.q = q def run(self): print("Starting " + self.name) threadfunc(self.name, self.q) print("Exiting " + self.name) # Create new threads for tName in threadList: thread = TimeThread(tName, workQueue) threads.append(thread) thread.start() # Fill the queue queueLock.acquire() for word in nameList: print("inserting word "+word+" into queue") workQueue.put(word) queueLock.release() # Wait for queue to empty while not workQueue.empty(): pass # Notify threads it's time to exit exitFlag = True # Wait for all threads to complete for thread in threads: thread.join() print("Exiting Main Thread")
In Python code:
Put the thread_function on a thread. Put 3 threads into a queue and run the threads.
import queue
queue = queue.Queue
def thread_function(name):
print("Thread %s: starting", name)
time.sleep(2)
print("Thread %s: finishing", name)
# start threads by passing function to Thread constructor
from pprint import pprint
import threading
import time
def threadfunc(*t):
print(">>>>",*t)
time.sleep(1)
print('[',*t,']')
time.sleep(2)
print("<<<<",*t)
arg1 = ("AAAAAAAA")
threadA = threading.Thread(target=threadfunc,args=arg1)
threadA.start()
arg2 = ("BBBBBBBB")
threadB = threading.Thread(target=threadfunc,args=arg2)
threadB.start()
arg3 = ("CCCCCCCC")
threadC = threading.Thread(target=threadfunc,args=arg3)
threadC.start()
threadA.join()
threadB.join()
threadC.join()
# multiple threads
import threading
import time
tnames = ('AAAAAAAA','BBBBBBBB','CCCCCCCC')
count = len(tnames)
threadlist = []
count = 3
def threadfunc(*t):
print(">>>>",*t)
time.sleep(1)
print('[',*t,']')
time.sleep(2)
print("<<<<",*t)
def threadList():
for index in range(count):
targ = (tnames[index])
thread = threading.Thread(target=threadfunc,args=targ)
print("inserting "+targ)
threadlist.append(thread)
for index in range(count):
print("starting "+tnames[index])
threadlist[index].start()
for index in range(count):
print("joining "+tnames[index])
threadlist[index].join()
threadList()
# thread inheritance
import threading
import time
tnames = ('AAAAAAAA','BBBBBBBB','CCCCCCCC')
count = len(tnames)
threadlist = []
count = 3
def threadfunc(*t):
print(">>>>",*t)
time.sleep(1)
print('[',*t,']')
time.sleep(2)
print("<<<<",*t)
class TimeThread (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
threadfunc(self.name)
for index in range(count):
print("inserting "+tnames[index])
threadlist.append(TimeThread(index+1, tnames[index]))
# Start new Threads
for index in range(count):
print("starting "+threadlist[index].name)
threadlist[index].start()
# Join Threads
for index in range(count):
print("joining "+threadlist[index].name)
threadlist[index].join()
print("Exiting Main Thread")
# thread queue
import queue
import threading
import time
exitFlag = False
def threadfunc(threadName, que):
print('>>>>'+threadName)
time.sleep(1)
while not exitFlag:
if not workQueue.empty():
data = que.get()
print('[',*data,']')
time.sleep(2)
print('<<<<'+threadName)
class TimeThread (threading.Thread):
def __init__(self, name, q):
threading.Thread.__init__(self)
self.name = name
self.que = q
def run(self):
print("Starting " + self.name)
threadfunc(self.name, self.que)
print("Exiting " + self.name)
threadList = ["AAAAAAAA", "BBBBBBBB", "CCCCCCCC"]
nameList = ["Alpha", "Beta", "Gamma"]
workQueue = queue.Queue(len(nameList))
threads = []
# Create new threads
for tName in threadList:
thread = TimeThread(tName, workQueue)
thread.start()
threads.append(thread)
# Fill the queue
for word in nameList:
print("inserting word "+word+" into queue")
workQueue.put(word)
# Wait for queue to empty
while not workQueue.empty():
pass
# Notify threads it's time to exit
exitFlag = True
# Wait for all threads to complete
for thread in threads:
thread.join()
print("Exiting Main Thread")
# queue lock
import queue
import threading
import time
exitFlag = False
threadList = ["AAAAAAAA", "BBBBBBBB", "CCCCCCCC"]
nameList = ["Alpha", "Beta", "Gamma"]
queueLock = threading.Lock()
workQueue = queue.Queue(3)
threads = []
def threadfunc(threadName, q):
print('>>>>'+threadName)
time.sleep(1)
while not exitFlag:
if not workQueue.empty():
queueLock.acquire()
data = q.get()
print('[',*data,']')
queueLock.release()
time.sleep(2)
print('<<<<'+threadName)
class TimeThread (threading.Thread):
def __init__(self, name, q):
threading.Thread.__init__(self)
self.name = name
self.q = q
def run(self):
print("Starting " + self.name)
threadfunc(self.name, self.q)
print("Exiting " + self.name)
# Create new threads
for tName in threadList:
thread = TimeThread(tName, workQueue)
threads.append(thread)
thread.start()
# Fill the queue
queueLock.acquire()
for word in nameList:
print("inserting word "+word+" into queue")
workQueue.put(word)
queueLock.release()
# Wait for queue to empty
while not workQueue.empty():
pass
# Notify threads it's time to exit
exitFlag = True
# Wait for all threads to complete
for thread in threads:
thread.join()
print("Exiting Main Thread")
The source code of the program
import queue
queue = queue.Queue
def thread_function(name):
print("Thread %s: starting", name)
time.sleep(2)
print("Thread %s: finishing", name)
# start threads by passing function to Thread constructor
from pprint import pprint
import threading
import time
def threadfunc(*t):
print(">>>>",*t)
time.sleep(1)
print('[',*t,']')
time.sleep(2)
print("<<<<",*t)
arg1 = ("AAAAAAAA")
threadA = threading.Thread(target=threadfunc,args=arg1)
threadA.start()
arg2 = ("BBBBBBBB")
threadB = threading.Thread(target=threadfunc,args=arg2)
threadB.start()
arg3 = ("CCCCCCCC")
threadC = threading.Thread(target=threadfunc,args=arg3)
threadC.start()
threadA.join()
threadB.join()
threadC.join()
# multiple threads
import threading
import time
tnames = ('AAAAAAAA','BBBBBBBB','CCCCCCCC')
count = len(tnames)
threadlist = []
count = 3
def threadfunc(*t):
print(">>>>",*t)
time.sleep(1)
print('[',*t,']')
time.sleep(2)
print("<<<<",*t)
def threadList():
for index in range(count):
targ = (tnames[index])
thread = threading.Thread(target=threadfunc,args=targ)
print("inserting "+targ)
threadlist.append(thread)
for index in range(count):
print("starting "+tnames[index])
threadlist[index].start()
for index in range(count):
print("joining "+tnames[index])
threadlist[index].join()
threadList()
# thread inheritance
import threading
import time
tnames = ('AAAAAAAA','BBBBBBBB','CCCCCCCC')
count = len(tnames)
threadlist = []
count = 3
def threadfunc(*t):
print(">>>>",*t)
time.sleep(1)
print('[',*t,']')
time.sleep(2)
print("<<<<",*t)
class TimeThread (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
threadfunc(self.name)
for index in range(count):
print("inserting "+tnames[index])
threadlist.append(TimeThread(index+1, tnames[index]))
# Start new Threads
for index in range(count):
print("starting "+threadlist[index].name)
threadlist[index].start()
# Join Threads
for index in range(count):
print("joining "+threadlist[index].name)
threadlist[index].join()
print("Exiting Main Thread")
# thread queue
import queue
import threading
import time
exitFlag = False
def threadfunc(threadName, que):
print('>>>>'+threadName)
time.sleep(1)
while not exitFlag:
if not workQueue.empty():
data = que.get()
print('[',*data,']')
time.sleep(2)
print('<<<<'+threadName)
class TimeThread (threading.Thread):
def __init__(self, name, q):
threading.Thread.__init__(self)
self.name = name
self.que = q
def run(self):
print("Starting " + self.name)
threadfunc(self.name, self.que)
print("Exiting " + self.name)
threadList = ["AAAAAAAA", "BBBBBBBB", "CCCCCCCC"]
nameList = ["Alpha", "Beta", "Gamma"]
workQueue = queue.Queue(len(nameList))
threads = []
# Create new threads
for tName in threadList:
thread = TimeThread(tName, workQueue)
thread.start()
threads.append(thread)
# Fill the queue
for word in nameList:
print("inserting word "+word+" into queue")
workQueue.put(word)
# Wait for queue to empty
while not workQueue.empty():
pass
# Notify threads it's time to exit
exitFlag = True
# Wait for all threads to complete
for thread in threads:
thread.join()
print("Exiting Main Thread")
# queue lock
import queue
import threading
import time
exitFlag = False
threadList = ["AAAAAAAA", "BBBBBBBB", "CCCCCCCC"]
nameList = ["Alpha", "Beta", "Gamma"]
queueLock = threading.Lock()
workQueue = queue.Queue(3)
threads = []
def threadfunc(threadName, q):
print('>>>>'+threadName)
time.sleep(1)
while not exitFlag:
if not workQueue.empty():
queueLock.acquire()
data = q.get()
print('[',*data,']')
queueLock.release()
time.sleep(2)
print('<<<<'+threadName)
class TimeThread (threading.Thread):
def __init__(self, name, q):
threading.Thread.__init__(self)
self.name = name
self.q = q
def run(self):
print("Starting " + self.name)
threadfunc(self.name, self.q)
print("Exiting " + self.name)
# Create new threads
for tName in threadList:
thread = TimeThread(tName, workQueue)
threads.append(thread)
thread.start()
# Fill the queue
queueLock.acquire()
for word in nameList:
print("inserting word "+word+" into queue")
workQueue.put(word)
queueLock.release()
# Wait for queue to empty
while not workQueue.empty():
pass
# Notify threads it's time to exit
exitFlag = True
# Wait for all threads to complete
for thread in threads:
thread.join()
print("Exiting Main Thread")
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 7 images