Welcome To Florida Sign
CSC 4410: OS and Concurrency
(Spring 2024)

Syllabus

LMS

Teachers


Assignments


Other Pages

Project 1:
Blocking Queue


Assigned: Fri Feb 16 2024
Due: 11:59:00 PM on Fri Mar 22 2024
Team Size: 1-3
Language: Java
Out of: 175 points


In this project, you will implement a blocking queue, which can handle multi-threaded calls to the add and remove methods. Important rules about what you're allowed to use/reference:
  • You are not allowed to use any already-defined concurrent data structures, neither built-in to standard Java packages nor available online. You are solving the concurrency here. If you violate this, you will get a zero for the project. (You are certainly allowed to use any non-thread safe structures such as LinkedList and ArrayList.)
  • You are not allowed to look up how to implement a concurrent queue or blocking queue or thread-safe queue in any programming language (including pseudocode). If you're stuck on something, review the lecture notes or come ask me questions. Violating this counts as a breach of the honor code and may incur administrative and academic penalties.
  • You should not use your MySemaphore.java code from the prior project, but instead use the built-in Semaphore class from the java.util.concurrent library.

Part 0, 0 points: Create a new Java file, MyBlockingQueue.java. Include a file header that includes the names of all teammembers. I recommend doing this as a Javadoc comment:

/**
 * Models a thread-safe Blocking Queue.
 *
 * @author Barbara M. Roberts <barbie@mattel.com>
 * @author Margaret Hadley <midge@mattel.com>
 */

Part 1, 0 points: Start your MyBlockingQueue class and, in good Java practice, include a generic type for the type of objects that will be stored in your queue. In case you haven't done that before, you can do it like this:

public class MyBlockingQueue<YourGenericType> {
But you should choose your own GenericType. It acts like a variable for a type. If you have questions about how to use generics, please ask me! (It's easier than you think and I can catch you up fast!)

Part 2, 0 points: Add a constructor that takes a single int parameter. This will be the maximum number of elements that can be inside your MyBlockingQueue at any time. Think about what fields you want your class to have and do all the appropriate initialization in the constructor. (You'll likely have to come back here and change things as you continue through the project.) You can then create blocking queues like this:

MyBlockingQueue<String> blockingStrings = new MyBlockingQueue<>(5);

Part 3, 0 points: Add a method, add, that takes a parameter of your generic type and adds it to the back of your queue. The signature should start off looking like this:

public void add(YourGenericType element) {

Part 4, 10 points: Add a toString method that prints out the elements in the queue.

Part 5, 50 points: Add a method, remove, that removes and returns the element at the front of the queue.

Part 6, 7 points: Add a method, getNumElements, that returns the number of elements currently stored in the queue.

Part 7, 8 points: Add a method, getFreeSpace, that returns the number of empty spots in the queue.

Part 8, 0 points: At this point, you should be able to compile and run your queue with my code: BlockingQueueTester.java. Most of the project points are indicated there, though there isn't a perfect test for every part! If you put that in the same folder as your code, you can test your code like this:

$ javac *.java
$ java -ea BlockingQueueTester
If you want to modify the number of threads for your own testing purposes, you can do that like this:
$ java -ea BlockingQueueTester 500
That would run the tests with 500 threads instead of the default 20,000. Please be aware that I will run it with the default amount!

Part 9, 10 points: Make sure that your queue is FIFO (First-In, First-Out).

Part 10, 30 points: Make sure that your remove method blocks appropriately: when there are no elements in the queue.

Part 11, 30 points: Make sure that your add method blocks appropriately: when the queue is full.

Part 12, 10 points: Make sure that your queue passes my stress tests. (There are some commented-out print statements you can comment-in as necessary while you're testing.)

Part 13, 20 points: Make sure that there are no race conditions or possible deadlocks in your project. You may or may not see this during testing, so be very careful. I will look carefully at your code!

Submitting your Project: Upload your MyBlockingQueue.java file to canvas. If you get it up before the deadline and send me a Slack message, I can run a test on my own computer. (There is no limit to the amount of times you can ask me to do this. The only barrier is how much time I actually have.) Make sure the names of all group members are in the code header.