Overview

In this assignment you will build on what you learned in labs and lectures to solve the bounded buffer problem in Java.

The bounded buffer problem

We have a buffer of a fixed size. A Producer thread inserts integers into the buffer, a Consumer thread takes them out. There are obvious constraints:

Assignment

Write a Java program that solves the bounded buffer problem. This will involve following the steps given below.

1. The BoundedBuffer

Write the BoundedBuffer class. The constructor should take a single parameter specifying the size of the buffer (passed on the command line) and should initialise the variables associated with the buffer. Some suggested variables include:

When roomAvailable is false the buffer is full, when true there is at least one free slot available for the Producer. When dataAvailable is false the buffer is empty, when true there is at least one integer in the buffer for the Consumer to remove. The buffer operates in a FIFO manner, i.e. the first item to go in is the first to come out.

Two pointers are associated with the buffer, nextIn and nextOut. nextIn points to a free slot in the buffer and indicates where the Producer will insert the next integer. nextOut points to the next item to be taken out of the buffer by the Consumer (thereby producing a free slot). You need to be careful that nextIn and nextOut do not grow beyond the length of the buffer but wrap around instead. Modulo arithmetic will take care of that problem for you. A counter variable, occupied, keeps track of the number of items currently in the buffer.

The class should implement two methods, insertItem and removeItem which will be called by the Producer and Consumer threads respectively.

2. The Producer

Write the Producer thread class. It should repeatedly produce a random integer between 0 and 100, insert it into the buffer and then sleep for a random duration between 0 and 100ms. (For debugging purposes, every time it adds an integer to the buffer it could print out a message saying so.)

3. The Consumer

Write your Consumer thread class. It should repeatedly consume an integer from the buffer and then sleep for a random duration between 0 and 100ms. (For debugging purposes, every time it takes an item from the buffer it could print out a message saying so.)

4. The Watcher

Add a Watcher thread which once every second wakes up and prints out the current status of the buffer: it should print out delta (ins - outs - occupied) which should always be zero along with the number of slots currently occupied.

5. Terminating threads

After the program has been running for 1 minute all threads should terminate with each printing a message saying it has finished. Reading the concurrency tutorial here will help you with this part of the assignment.

6. Keeping statistics

After the threads have terminated the program should print out the average length of time (in ms) between the Producer generating an integer and the Consumer removing that integer from the buffer.

7. Putting it all together

Lastly, write the Assignment01 class which launches your threads and which, after 1 minute is responsible for shutting them down.

Deliverables

You are required to deliver the following:

Marks are awarded for solving the assignment (it will be tested), solving it efficiently and demonstrating your understanding of the problems, issues and solutions through comments. It is particularly important that code which handles concurrency-related issues is well-commented. Any shortcomings in your solution must be clearly identified. Finally, your code must be neatly formatted, contain no lines longer than 80 characters and contain no commented-out code.

What's it worth?

15% of your overall mark.

Do I have to work in a team?

Yes. Only submissions by teams of two or three people are acceptable. Single submissions or submissions from teams with more than three members will not be marked.

Can I borrow code from elsewhere?

See our policy on plagiarism. Submitting your assignment indicates that you have read and agree with the declaration in Appendix A of the plagiarism policy.

I do however strongly suggest you read the Java concurrency tutorial available from here. It will cover more than you need to know.

FAQ

Q. I cannot find a partner for the project. What should I do?
A. E-mail me as soon as possible and I'll try to find you one. If you leave e-mailing me too late I will not accept your submission and you will receive a mark of zero.

Q. I am an Erasmus student and am taking this module. Do the usual delivery instructions (see below) apply to me?
A. No. Tar your solution and e-mail it to me by the due date.

Q. I was sick over the period we had to work on the assignment. What can I do?
A. Submit a doctor's note to the faculty office and to me covering you for the period in question. The programme board will then make a decision regarding your eligibility to repeat over the summer.

Q. What would a working solution look like?
A. It might produce output like this. Note output may vary significantly from one run to the next.

Q. Can I use semaphores, locks etc.?
A. No. You may only use the usual synchronized wait, notify and notifyAll set.

Q. Can I use thread.stop to terminate my threads?
A. No. thread.stop is a deprecated method (i.e. Sun do not recommend you use it because it does not work properly).

Q. I'm having trouble terminating my threads. What should I do?
A. Your priority should be, first off, to solve the bounded buffer problem as described above with a solution that runs indefinitely. You will get marks for doing so. After that you can move on to adding the thread terminating code.

Q. I'm having trouble generating the statistics. What should I do?
A. Your priority should be, first off, to solve the bounded buffer problem as described above with a solution that makes no attempt to generate statistics. You will get marks for doing so. After that you can move on to adding the statistics-generating code.

I have a different question

E-mail me. I will not reply if your question is answered in the FAQ.

How to submit

Create the following directory off one team member's Linux home directory on a CA lab machine:

~/ca216/assignment01 (note the zero)

Place all deliverables in this directory. Each must contain the names and ID numbers of those students responsible for producing it. The contents of this directory will be automatically collected immediately the deadline expires. You will lose write permission on the directory at this time. Also, a file named collected will appear in the directory and serves as a verification that your assignment was successfully collected. If it does not appear, your assignment was not successfully submitted.

It is your responsibility to ensure the directory exists only in one team member's account, is correctly named (directory and filenames are case sensitive) and contains your solution to the assignment. Failure to comply with any of these requirements will result in an automatic mark of zero. To create the directory enter the following in a command shell:

Deadline

Your solution will be collected from the above directory at 18.00 Friday 9 April 2010.