Assignment #1: Threads and Critical Sections

This is a tricky assignment that illustrates threads, synchronization, and critical sections. You will write a simulation of the Ticketmaster Phone system. Here we will assume that there are five lines, but only three operators. When a phone call arrives, we see if there is a free line. If so, the phone rings; otherwise it is busy. Once the phone rings, we wait until there is a free operator, and when we get one, we pretend to get service. Each phone call is represented by a thread, and you will have more threads running than phone lines. There are several components to the project.

Semaphor

First you will complete the Semaphor class. Semaphor.java is your starting point. A Semaphor is use to control entry to a critical section with a limit of how many threads are allowed to be simultaneously active. To enter the critical section you must gain ownership with acquire, and upon leaving the critical section you should release so a waiting thread can enter. The Semaphor maintains the limit of threads concurrently allowed in its critical section, and a list of the threads currently (in an ArrayList) in the critical section. The skeleton is already written, you need to follow the embedded comments to complete the code.

The PhoneCall Class and main

A phone call is represented by a PhoneCall class that contains a caller's id as private data member, set in its constructor. PhoneCall will extend Thread and provide a run method. It will also contain a host of static data members, including the semaphor and a lock that are both shared and control the synchronization. To start with, implement a constructor, and provide a run that simply prints a message, so you know that the thread is running, sleeps for a few seconds (use Thread.sleep), and then prints a message before the thread ends. Later you will do the meaty part of the simulation.

Your main routine will declare an array of threads that will represent each phone call. Then, write a loop that constructs PhoneCall objects to initialize the array. Then, write a loop that starts each PhoneCall thread. When main terminates print a message that the simulation is over. If you run the program, you'll notice that the message comes early. You will want main to block until all the other threads complete and to do that, you should have another loop that uses join on each thread.

The Synchronization

Once you have the threads going, you'll want to do the real part of the simulation. Your PhoneCall class should declare the following static data:
 private static int NUM_OPERATORS = 3; private static int NUM_LINES = 5; private static int connected = 0; // Callers that are connected private static Semaphor operators = new Semaphor( NUM_OPERATORS ); 
The variable connected tells how many callers are currently connected (a call attempt is declared busy if connected==NUM_LINES). Access to connected must also be controlled by a critical section. The operators semaphor is used once a call connects. Here is a sketch of the run method for PhoneCall:
  1. Print that an attempt to connect has been made
  2. Check if the connection can be made
  3. Wait for an operator to be available (use a semaphor)
  4. Print a message that the order is being taken
  5. Simulate an order by sleeping for a few seconds
  6. Print a message that the order is complete (and update the semaphor)
  7. Update connected (do you need a critical section?)
  8. Print a message that the call is over

Synchronization Rules

You may not use classes in java.util.Concurrent package or related packages.

Test Program

Test your program by running with 10 phone calls. In main, delay the last two phone calls enough so that the line will not be busy.

Submission Details

  1. All assignments will be submitted through moodle. Check early to make sure you have access to the COP-4338 Moodle site. We will not use moodle for anything else this semester.
  2. Create a folder with your student ID and include all the files you want to submit in the folder (maintaining package hierarchy if appropriate). Include also the following three files:
    1. README.txt: contains the compilation instructions,
    2. OUTPUT.txt: contains the output of the program. If there are multiple runs; they should be combined into this file, and be easily printable and understandable.
    3. BUGS.txt: indicates whether the program is 100% working, or is not working. If not working, explain what parts work -- the better you do this, the more likely you can get partial credit for what works.
  3. At the beginning of your code, please also include your name and student ID.
  4. Compress the folder into a zip file and upload the compressed file in moodle. Name the file using your Panther ID (e.g. 1234567.zip).
  5. It is the student's responsibility to submit homework successfully.
  6. If you encounter submission difficulties, please email the teaching assistant Yangyang Wu at: yangyangbg@gmail.com