University of Washington, Bothell
CSS AP442: Advanced Programming Methodology I
Autumn 1998
Homework Assignment 2: Concurrency and Synchronization
Assigned: 3 November 1998
Due: 15 November 1998 by midnight
This assignment will give you experience using concurrent processes to perform a simple simulation. In this simulation, two opponents on mountain bikes with different characteristics are competing in a race. You will place each competitor in a separate thread, and will need to synchronize their activities.
The race course is 700 meters long, and winds up and down a mountain, so occasionally one or the other cyclist will lose ground. You needn't worry about the course topography, however, as you will use random number generation to determine how each is progressing. Instead, divide the race into 70 10-meter segments, with the competitors starting in segment 0 and the finish line being segment 69. For simplicity's sake, just think of the course as being a linear arrangement of segments.
Each second, the competitors' threads will be notified to recompute their location in the race according to the following rules:
| Competitor | Action | Probability | Result |
| 1 | Sprint | 0.5 | +3 segments |
| Grind | 0.3 | +1 segment | |
| Slip | 0.2 | -6 segments | |
| 2 | Sprint | 0.2 | +9 segments |
| Glide | 0.3 | +1 segment | |
| Rest | 0.2 | 0 segments | |
| Slip | 0.2 | -2 segments | |
| Fall | 0.1 | -12 segments |
You may decide to use different classes for each competitor, or use
one class and create two objects whose ``move probability tables'' are
loaded at construction time (or some similar idea). In either event,
each player's object should contain a thread which tracks and updates
its race position. At no time may a player's position be less than
zero or greater than 69; if a player slips or falls and would move
back past zero, just move that player to segment zero, and the first
player to reach segment 69 wins. You may compute the probabilities by
generating integers in the range of
.
Begin the race by printing: They're off!!!. Thereafter, each player gets one move per tick of the race clock. For each tick, after both bicyclists have moved, you should determine the location of each and print two lines that have a 1 or 2 in the column that corresponds to his/her current segment, and are blank elsewhere. You should then test to see if either contestant has won, and print out a message like Cyclist 1 wins!. Note that there can be ties. If neither cyclist has won yet, their threads should be notified to compute a new position.
Write a program which simulates a two-contestant mountain bike race, with each cyclist placed in separate threads. At the start of the race, call each thread's start() method. Use wait(), notify(), and notifyAll() to synchronize the contestants' activities.
Design and implement the program so that an arbitrary number of cyclists can compete. Your program should take the number of competitors as its sole command line argument. The race status output should display one line per competitor. The probability tables for the additional cyclists should be generated randomly, such that the weighted sum for each (sum of probabilities times corresponding moves) is between 0.2 and 0.8 (average number of segments per time step).
You will be submitting your source code separately from your documentation. Please email your source code to cssap442@u.washington.edu. You may submit an entire directory structure as a zip or tar file attachment, in which case your subject line should read, for example: ID#.zip, where ``ID#'' is replaced by your student ID number. Please ensure that long file names are preserved.
Alternatively, each file can be sent in a separate email message as an attachment or in the body, with the subject line: ID# subdirectory/filename.java, showing the subdirectory name for any package files. Please ensure that no lines get wrapped or truncated!
Any email sent after the due date/time will be counted as a late submission. You will receive one automated response, which you should save as a receipt of submission. Please send your code only once; multiple submissions will slow our turn-around time to everyone.
Your program must be called Dirt; i.e., main() should be in a class defined in Dirt.java. Please submit your documentation as hardcopy in the next class meeting after the due date. Include information about the status of your assignment, if it is not completely working.
This document was generated using the LaTeX2HTML translator Version 98.1p1 release (March 2nd, 1998)
Copyright © 1993, 1994, 1995, 1996, 1997, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
The command line arguments were:
latex2html -split 0 assn2.
The translation was initiated by Dr. Michael Stiber on 1998-11-03