CSC 325

PROGRAM 4

Due December 16, 2015

Multithreading

I. Requirements:

Using the code from the project chapter21\MicrosoftMonitorSample\MicrosoftMonitorSample.csproj,create a C# program that more realistically simulates the consumer/producer situation:

  1. Add a synchronized method to the class Cell to check if the cellContents field reached the maximum capacity (interpretation - the warehouse is full)Invokethis method in the WriteToCell methodto stop producing new products and to wait until the cellContentsfield has been decrementedby the consumerthread below this maximum capacity.
  1. Addanother synchronized method to the class Cellto check if the cellContentsfield reached the minimum capacity (interpretation - the warehouse is empty). Invokethis method in the ReadFromCell methodtostop consuming new products and to wait until the cellContentsfield has been incrementedby the producer thread above the minimum capacity.
  1. Add an int parameter to the WriteToCell method to represent the number of items manufactured by the producer (interpretation – the producer may produce more than one item) and adjust the loop in the ThreadProdRunmethod to callthe WriteToCell method passinga different int parameter valueeach time(e.g. use random selection)in the range from the minimum cell capacity defined in the requirement 2 above to the maximum cell capacity defined in the requirement 1 above.Select the number of the loop's iterations so that your testing produces meaningful results. Adjust the WriteToCell method so, that every time the method is called, the cellContentsfield is incremented by theint value (representing the number of items manufactured by the producer) passed as an argument to this method. Make sure that the maximum capacity is not exceeded.
  1. Add an int parameter to the ReadFromCell method to represent the number of items consumed by the consumer (interpretation – the consumer may consume more than one item) and adjust the loop in theThreadConsRunmethod to callthe ReadFromCell method passinga different intparameter valueeach time (e.g. use random selection) in the range from the minimum cell capacity defined in the requirement 2 above to maximum cell capacity defined in the requirement 1 above.Select the number of the loop's iterations so that your testing produces meaningful results. Adjust the ReadFromCell method so, that every time the method is called, the cellContentsfield is decremented by the int value (representing the number of items consumed by the consumer) passed as an argument to this method. Make sure that the cellContents field does not fall below minimum capacity.
  1. Modify the Main()method to allow to start the consumer and producer threads in any order.
  1. Test your code to make sure that the synchronization works and that both the consumer and producer threads don’t violate minimum/maximum capacity requirements irrespectively of the order these threads start.
  1. Justify and include into the report the selection of the synchronization techniques for the methods created to satisfy requirements 1 - 4 above.
  1. Your code should provide detailed output of the execution results.

II. Bonus tasks (each can add up to 20% to the test grade):

  1. Enhance and test your code to make sure that it works with at least 5 producer threads and 3 consumer threads started in any random order.
  1. To simulate real life production/consuming/deliverydelays, add Thread.Sleep method calls to the ReadFromCell and WriteToCell methods. Test your code with at least 3 sets of different sleep times (the sleep times for ReadFromCell and WriteToCell methods should not be the same) and present the analysis of the effect of delays (sleep times) on the behavior of the consumer/producer models.

III. Hints:

  1. Main() method shouldinvoke the consumer/producer threads both in the producer/consumer and reverse consumer/producer order.
  1. The maximum/minimum capacity fields may be declared as constants.

IV. Notes:

  1. When creating project using a computer in the Computer Science Department laboratories (MH 202/209/210), always make sure that the project and all associated files are on the disk drive that you will be able to access next time. This drive may be h: drive on the Computer Science Department LAN, USB flash drive, etc. Never store any files on the local drives c: or d: as these files will be automatically deleted after you log off.
  1. When submitting lab or programming assignments, always follow the home work submission guidelines described in the Laboratory/Program Report Specifications document CS325Report.doc that can be downloaded from my Web site
  1. Save your work regularly, especially at the end of each class.
  1. Keep a detailed record of all steps performed.
  1. The source code used in the project chapter21\MicrosoftMonitorSample\MicrosoftMonitorSample.csproj is a modification of the code in the Example 2: “Synchronizing two threads: a producer and a consumer” at

V. To download sample source codefrom the textbook:

  1. Go to
  1. Click on the link labeled “CSC 325” and then on the link “Source code (7-Zip)”. Download the file source.7z to your computer.
  1. Using WinZip, 7-Zip or any other utility unzip the source.7z file.