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")

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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")

 

 

 

Expert Solution
Step 1

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 7 images

Blurred answer
Knowledge Booster
Avoiding deadlock
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education