Lab 2: Halloween Candy Factory using Threads and Semaphores

Due: Thu, Oct 21, 2010, 21:00 hrs
Web posted: Wed, Oct 06, 2010, 17:00 hrs



Getting Started

The Assignment

For this assignment and all subsequent ones, you may work in groups.
  1. Producer Consumer using Threads and Semaphores
    In this assignment, we will use Java threads and semaphores to implement a useful application based on the Producer Consumer problem studied in class.


    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:

    Your code should prompt for the above inputs in that order. You can assume that a valid integer is provided by the user for each of these.

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


How to Turn in Lab 2

All of the following files must be submitted on SPARK as a zip file to get full credit for this assignment.
  1. Your zip file should contain a copy of all source files.
  2. Your zip file should contain a copy of a README file identifying your lab partner (if you have one) and containing an outline of what you did for the assignment. It should also explain and document your design choices. Keep it short and to the point.

  3. If your implementation does not work, you should also document the problems in the README, preferably with your explanation of why it does not work and how you would solve it if you had more time. Of course, you should also comment your code. We can't give you credit for something we don't understand!
  4. Fnally, your zip file should contain a copy showing sample outputs from your program runs.
  5. Individual Group Assessment (for students working in groups only)
  6. 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...
  7. Note: We will strictly enforce policies on cheating. Remember that we routinely run similarity checking programs on your solutions to detect cheating. Please make sure you turn in your own work.

    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.

  8. Grading scheme for Lab 2
  9. Late Policy: Please refer to the course syllabus for late policy on labs assignments. This late policy will be strictly enforced. Please start early so that you can submit the assignment on time.