10.6. Lab 2 - Multithreaded Programming and Synchronization¶
Based on the documentation and sample code in Multi-threaded Programming and Bounded Buffer (Producers and Consumers), develop a simulation program of the bounded buffer problem with 10 containers, 6 producers and 6 consumers.
Each producer or consumer should run in it’s own thread.
Each time a product is produced or consumed, use a random number generator and the
sleep()
method from thetime
module to cause the thread to sleep for a random amount of time between 1 and 10 seconds.Hint
Here is a small piece of code that loop for 5 times and each time sleeps for a random about of time.
import random import time for i in range(5): x = random.randrange(1, 10) print("x = {}".format(x)) time.sleep(x)Cause each producer and consumer to print when it acquires a container or deposits a container.
Maintain a shared buffer that tracks and prints how many containers have been produced and consumed. The producers and consumers should each stop after 30 items have been either produced or consumed.
Submit a short lab report as a plain text file explaining what you did in the activity and what you learned from doing it. Include your source code in your assignment submission.