Getting Started
Amherst Candy Factory is preparing up for Halloween and has
implemented an assembly line to ramp up Halloween candy production.
The assembly line will be implemented using a bounded buffer
producer and consumers.
The factory contains two types of worker threads: producers and consumers. Each producer thread produces a certain type of candy , while each consumer creates boxes of assorted candy using ones produced by producers. After producing each candy, the producer deposits into the bounded buffer. A consumer extract candy from the bounded buffer, one at a time, and when i candy items have been extracted, it fills up a box of assorted candy.
Your program should accept the following inputs:
To begin, you will need to spawn m Producer threads and n Consumer threads. Assume that each producer threads produces candy of a unique color and that candy items are sequentially numbered. Thus, Producer 1 might produce items: blue 1, blue 2, blue 3..., while Producer 2 will produce items: red 1, red 2, red 3, ..., and so on. You can assign any color of your choice to each producer thread. After producing each item, the producer will deposit it into the bounded buffer array (assembly line). If the buffer is full, it must wait until one slot becomes available.
Each consumer thread should extract one candy item from the
buffer. After retrieving i candy items, it should print the
contents of the candy box as follows:
Consumer thread A filled a new box of candy containing: blue 3,
red 7, green 8, ...
You will need to implement your own Bounded Buffer as in Lecture 9, You are allowed to use Java's Semaphore class (java.util.concurrent.Semaphore) alone for synchronization. Do not use Java monitors in your solution.
Finally, you will also need to maintain a shared counter variable initialized to m. When each Producer thread finishes (i.e., is done producing j candy items), it will decrement the counter by 1 and exit. If the counter equals zero, and if bounded buffer is empty then the Consumer threads should quit. Be sure to protect this shared counter with a mutex semaphore.
Once you have written the code for this problem and have tested it for correctness, you should run the following tests and document your observations:
Start your program with 4 producers and 1 consumer. Run the program a few times (if possible do this on two machines, say the edlab PC and your laptop/PC). Does the ordering of the output change? Next run the program with a background job running on your computer (e.g., a browser playing a youtube video or a simple program that does infinite loops). Now run the program a few times and observe the ordering of the output.
Next repeat the above tasks with 4 producers and 3 consumers. Be sure to document your observations and provide an explanation about why a multi-threaded program may not produce deterministic output even though the input does not change across multiple runs.
Also document any hard problems you faced during debugging of your multi-threaded or syncronization code (and state whether non-deterministic program behavior made the task of debugging your code harder).
A percent of your lab grade will come from your participation in this project as a member of your group.
What you need to turn in (each person individually):
Include in your zip file a copy of your assessment of the division of labor in the group in the format shown below. For a 2 person group, if you give yourself a 50% and your partner gives you a 50%, you will get the full credit for group participation. If you give your partner a 40% and your partner gives himself or herself a 40%, he or she will get fewer points for group participation. And so on...
You should be very careful about using code snippets you find on the Internet. In general your code should be your own. It is OK to read tutorials on the web and use these concepts in your assignment. Blind use of code from web is strictly disallowed. Feel free to check with us if you have questions on this policy. And be sure to document any Internet sources/ tutorials you have used to complete the assignment in your README file.