C SC 340 Project 3: Java Consultant Chat Room Simulation
Spring 2009
Due: before lab Tuesday May 12
Worth 15 of your 100 project points
Overview
A Java guru operates an Internet chat room for computer science students ('followers')
who wish to discuss the intricacies of Java multithreaded programming. No more
than one follower is allowed in the chat room with the guru at any given time.
If the chat room is busy, a newly arriving follower will be added to a waiting
room of fixed capacity. If the waiting room is full, the follower gives up. Anytime
the chat room and waiting room are both empty of followers, the guru takes a nap
and must be awakened by the next arriving follower. Your program will simulate
the chat room operation using Java threads.
The Cast
Note that each of these will be implemented as a Java class.
- Chat : The narrator. There will be one Chat object. Chat must implement Runnable, thus it will be used
as a thread. The main() method lives here. Its run() method will create the other characters.
- Guru : The Java guru. There will be one Guru object, created by the Chat. Guru is not a thread. Only one follower can be in the
chat room with the guru at a time.
- WaitingRoom : The guru's waiting room. There will be one WaitingRoom object, created by the Chat.
WaitingRoom is not a thread. Its built-in wait set will represent seats for waiting followers.
- Follower : Followers of the Java guru. There will be many followers, each with a unique
identifier, created by the Chat. Follower must implement Runnable, thus it will be used as a thread. Each follower
wants to chat with the guru but will give up if the waiting room is full.
- You are free to create additional classes as needed for your solution.
Specifications, Details and Requirements
Preliminary: simulation of time
- This is a simulation, so we have the concept of time and a clock. Think of the clock as nothing more than an int variable
that has to be updated periodically.
- The passage of time is simulated by the
Thread method sleep().
This static method takes one argument, the number of milliseconds for the current thread to suspend itself.
- For this simulation, each millisecond of sleep time will simulate one minute of elapsed time.
- Thus the statement Thread.sleep(60); suspends the thread for 60 milliseconds, which represents 1 hour of time.
- The clock variable will be kept by the Chat object, and updated each time it awakes from its periodic sleep. It
will be updated by varying amounts since this sleep time is randomly determined.
Chat
- implements Runnable
- Contains main(). Main will expect one command argument, the waiting room capacity. Main will create and
start the only Chat object.
- Contains a constructor. This constructor will have one parameter, the waiting room capacity which main() will
pass to it.
- Contains run(). Run will create the Guru object and the WaitingRoom object. Then it will perform a loop
where in each iteration it will sleep for awhile then create and start one Follower object.
- Details on each iteration of the run loop:
- The Chat object will sleep for a random period of time ranging from
10 to 40 minutes. See the sections on Simulation of Time, above, and Generating
Random Values, below. This is simulated by 10 to 40 milliseconds sleep.
- Upon awakening, it will update the clock by adding the sleep time to its value.
- Then it will prepare to create a new Follower. This involves determining how much chat time the
new follower will need with the guru. Chat time is random between 10 to 30 minutes.
- Then it will create a Follower object, assigning its chat time as a constructor argument, create
a Thread from it, assign its
ID ("1", "2", "3", etc) using the Thread method setName(), then start it.
- The loop will terminate when 24 hours or more of time has elapsed. This
is represented by 24*60 = 1440 milliseconds of accumulated sleep time.
- There are more details on generating random values, below.
Guru
- There will be one Guru object, created by the Chat object.
- This class is not a thread
- It needs to keep track of whether it is napping or chatting.
- Only one follower can be chatting with the guru at a time.
- It may require methods for communication with followers and/or the waiting room.
- It will print various status updates as described below.
WaitingRoom
- There will be one Guru object, created by the Chat object.
- This class is not a thread
- It needs to keep track of it's capacity. This is provided as a constructor argument by the Chat object.
- It needs to keep track of its population. This is the number of followers who are waiting in it.
- It will not have a Collection object to represent its seats! These will be represented by its
built-in wait set. Follower threads get into the wait set by calling wait() on the waiting room object,
and leave it when another thread calls notify() on the waiting room object.
- It may require methods for communication with followers and/or the guru.
Follower
- implements Runnable
- Each follower is created by the Chat object. Its constructor will have one parameter, the desired chat time.
- Everything else happens in its run() method.
- The follower will first check the waiting room.
- If the waiting room is full, give up.
- If the waiting room is not full but not empty, take a seat by calling its wait() method. This will suspend
the follower and add it to the waiting room's wait set.
- If the waiting room is empty, check to see if the guru is chatting or napping. If chatting, take a seat in the
waiting room as described in the previous step. If napping, then wake the guru up and have your chat.
- The chat itself is simulated by having the follower call Thread.sleep() for the desired chat time. This was
provided by the Chat object to the follower's constructor.
- The follower will also print various status updates as described below.
Execution trace output
Produce output at selected points to demonstrate that the system works properly.
- Print a message when a new follower is created. Include the current time and follower ID.
- Print a message when a new follower doesn't try because waiting room is
full. Include follower ID and waiting room population.
- Print a message when a new follower is placed in the waiting room. Include follower ID and waiting room population.
- Print a message when a follower begins chat with guru. Include follower ID and chat time.
- Print a message when a follower ends chat with guru. Include follower ID.
- Print a message each time the guru starts to nap (note: this is not
done using sleep()!)
- Print a message each time the guru is awakened.
- The more information you print, the better. Consider the above to be minimum requirements.
You will probably need to adjust some of the system parameters to get a nice variety
of results. The values given, 10-40 for interarrival and 10-30 for chat, will
usually result in very small waiting room populations.
Coordinating threads
Use the Java monitor facilities to coordinate all thread activities.
- synchronized methods to implement critical sections. Use only
when needed, however, because unnecessary synchronization adversely affects
system performance.
- wait() and notify() to handle waiting in the list. These are
described in Object class documentation.
- Declare shared variables to be volatile.
- Do not use suspend() and resume().
These are deprecated Thread methods.
Generating random values
- Use two
java.util.Random
objects; one to generate Chat sleep times, and one to generate Follower chat
times.
- The former is known as interarrival time, the time that elapses between the arrival of new followers.
- The latter is known as service time, the amount of chat service that each follower requires.
- To get a random number in the range 10 to 40, use the one-parameter version of the nextInt() method to
get a value in the range 0 to 30, then add 10 to the result. Note the correct argument would be 31, not 30, because
the generated value is exclusive of the high end of the range.
- Read the section below on Reproducible Behavior
Documentation
- Be sure to put your name, course, and date in every file you create.
- Also document each class and method with comments.
Reproducible Behavior
Follower interarrival times and service (chat room) times are both based on random
values. For program testing purposes, you will want to use the same sequence of
random values from one run to the next. To achieve this, you need to know a little about how random number generators work.
Random number generators are more accurately called pseudo-random because they are not truly random. Every time the generator is called
to generate a number, it calculates the number by applying a formula to its previously-generated number. So how is the first random number generated?
It is based on what we call the seed value. If no seed is provided, the default seed is typically based on the computer's clock value. But if you provide
a specific seed value, then you control the number sequence. The same sequence will be produced every time you run the program. This is important when
testing multi-threaded software because it provides consistency in an otherwise unpredictable testing scenario!
I recommand this technique. Use
java.util.Random
objects with fixed seed values. Create one Random object to
generate interarrival times and a second one to generate service times. Generate
the service requirement at the time each follower object is created, and store
this value as a follower instance variable. If you follow these recommendations,
all random number generation occurs in the same thread (Chat) and both sequences
are guaranteed to be reproducible. Be sure to remove the seed for final testing
and project submission.
Scoring
The maximum 15 points are broken down as follows:
- 8 points maximum for observably correct behavior based on printed output.
Here are some (but not all) examples:
- followers go into waiting room if guru is busy and seats are available
- followers give up if waiting room is full
- followers go directly into the chat room if the waiting room is empty
and the guru is napping
- a follower is never in the waiting room and chat room at the same time
- waiting room never has population greater than its capacity
- guru is never napping while followers wait
- guru is never awake when the chat room and waiting room are both empty
- there is never more than one (or N, for the bonus) follower in the chat room at a time
- time between new followers is within correct range (subject to adjustment)
- time in chat room is within correct range (subject to adjustment)
- 3 points maximum for correct use of thread synchronization (wait, notify, synchronized)
- 2 points maximum for program structure
- 2 points maximum for documentation
Extra Credit
For up to 20% (3 points) extra credit, allow as many as N followers to be in the chat
room simultaneously. N would be entered as a second run argument.
To Turn In
Send all Java files as email attachments to psanderson@otterbein.edu.
[ C
SC 340 | Peter
Sanderson | Math Sciences server
| Math Sciences home page
| Otterbein ]
Last updated:
Peter Sanderson (PSanderson@otterbein.edu)