
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 nowThis is a popular solution!
Step by stepSolved in 4 steps with 7 images

- JAVA PROGRAMMING You are required to create a main class to apply ExecuterService with SingleThreadExecuter. The variable i is integer number and MUST be input from the keyboard. Output should be like this: Please input i: 3 pool-1-thread-1: 1 pool-3-thread-1: 1 pool-3-thread-1: 2 pool-3-thread-1: 3 pool-2-thread-1: 1 pool-2-thread-1: 2 pool-2-thread-1: 3 pool-1-thread-1: 2 pool-1-thread-1: 3 Total = 9Below is the part of code.class Counter public class Counter { private int counter; public void increment(){ counter++; } public void decrement(){ counter--; } synchronized public void myIncrement(){ counter++; } synchronized public void myDecrement(){ counter--; } }arrow_forwardWrite a Java program using Thread with single Server and Multiple Clients. NOTE: Client must be running until Bye message is sent. Server should never stop. I need answer question plsarrow_forwardA class that extends Thread must override ____ method ? start() run() init() sleep()arrow_forward
- Hello Can you please help me with this code because I am struggling how do to this, can you please help me this code has to be in C. Write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, and the third will determine the minimum value. For example, suppose your program is passed the integers 90 81 78 95 79 72 85 The program will report The average value is 82 The minimum value is 72 The maximum value is 95 The variables representing the average, minimum, and maximum values will be stored globally. The worker threads will set these values, and the parent thread will output the values once the workers have exited. (We could obviously expand this program by creating additional threads that determine other statistical values, such as…arrow_forwardIn computer graphics when looking at synchronizations when building a renderer in engine development, what are moments where locks and unlocks from mutex's would not always work when looking at synchronization for a renderer in an engine?What I mean is when developing a game engine and you'll want there to be synchronization from a renderer thread and a main thread. Where renderer thread handles flushing and executing all of the concurrent tasks, whereas main thread would handle more of the querying tasks to a render command queue, and handling how these operations are operated within the engine.Can you give me some ideas on when mutex's may not be enough in a more complex? Where you may have multiple scenes, where animation physics are applied, and various vast ideas in this perspective?On another can you give me in great details what are uses of ref counters and different variations of implementations (in C++), and why reference counters are used? And what purpose do they have, as…arrow_forward-0-0- thread (const thread&) = delete; thread& operator=(const thread&) = delete; void swap(thread& _Other) noexcept { _STD swap(_Thr, _Other._Thr); } _NODISCARD bool joinable() const noexcept { return _Thr._Id != 0; x void join() { if (!joinable()) { _Throw_Cpp_error(_ } if (_Thr._Id == _Thrd_ _Throw_Cpp_error(_ } if (_Thrd_join(_Thr, n Exception Thrown Exception thrown: read access violation. this was nullptr. _Thr = {}; Copy Details | Start Live Share session... ✔ Exception Settings ✔ Break when this exception type is thrown Except when thrown from: Reservation System.exe Open Exception Settings Edit Conditions _Throw_Cpp_error(_Code: _NO_SUCH_PROCESS); ох E..arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





