COMP 10183 Assignment 2
Fall 2023 Prepared by: Karen Laurin & Dave Gilbert
Overview
In this assignment you will be demonstrating your knowledge of creating and maintaining threads and using mutual exclusion techniques taught in class to protect critical sections of code to prevent data races.
Assignment #2 i s a more complex teams based race. In Assignment #1 you just need ed to track how long it takes 20 racers to cross the g orge. In this assignment, the racers are divid ed up into 5 teams of 4 racers eac h, and they must cross the gorge in both direction.
The Problem
You will be simulating adventure racing teams moving across a gorge on a ropes course. Each adventure team consists of four members, who must all cross the gorge, the team may then claim the ropes course medallion and then all four team members must cross the gorge back to continue on in the race.
The gorge consists of three rope crossing points. There is no way to pass another racer on the rope and each rope can only support one racer for safe ty, so a racer may only enter a rope if it is empty .
You do not need to worry about starvation for this a ssignment. If an adventure racing team or single racer has to wait some time for their turn on the rope that is ok, this is a race, no need to try and implement fairness or sharing .
For this assignment you sho uld not assign racers to ropes in advanc e. Because racers must go in both directions, and the race will be won by the team that finishes first, pre -assigning racers to ropes would create a major disadvantage.
The Design
The design for this simulation should follow the basic UML class diagram given below. You may add any variables or methods you feel are necessary to solve the problem. Notice t hat this design includes 5 AdventureTeams. You wi ll need a total of 25 threads, a thread for each racer, and a thread for each racing team. The racing team can make de cisions about which rope to select.
For all threads to see the three RopeCrossing objects, you may declare these three o bjects as static in your main thread (the RaceSimulation class).
Concurrency and Performance
In the main method, 20 racers will arrive at the beginning of execution. Each rac ing team needs time to set up their gear, which can randomly take between 200 and 400 milliseconds . Once a team is ready, racers can begin crossing. Each racer must individually select a rope to cross . No racer may make a return crossing until all their team mates have crossed .
The racer will take between 400 and 600 milliseconds to cross the gorge on the rope, which will be det ermined randomly .
5 racing teams concurrently set up their equipment: Average time = 300 ms
20 racers use 3 ropes to cross the gorge. Average time = 20 * 500 ms / 3 = 3.3 seconds.
20 racers use 3 ropes to re-cross the gorge. Average time = 20 * 500 ms / 3 = 3.3 seconds.
Total race time should be at least (2 * 3.3s + .3s) = 6.9 secs, usually it will be a little longer .
The JVM may not award the rope’s lock to the racer’s in a FIFO (first -in-first-out) manner. This is correct behaviour and does not need to be changed or managed. It is luck of the draw if the racer is able to grab the rope when available for their direct ion.
You will need a way to track when all four members have crossed the gorge and the team can claim their medallion . Consider using a simple atomic integer counter owned by the team.
To obtain full marks, communication between the threads should be using wait – notify, not busy waiting . The use of timed sleep in the code is only to represent the racer doing something (setting up or crossing the rope). Solutio ns that rely on busy waiting will receive a penalty.
Once all four team members have made it across, the team may claim their medallion and begin to cross back. Make sure the team members do not start crossing back until all members have crossed in the first direction. Each racer must individually select a rope to cross and it will take the racer a random amount of time. Their second crossing time may be different from their first .
Once the team has completed the round trip crossing, they should print their success to the terminal and then all threads corrrespoding to the team and it’s racers may terminate. As ea ch racer completes the round trip, you many terminate that individual thread for that racer.
Your program should track and display the following, in order to demonstrate correct performance:
• Time it takes each team to set up it’s gear • Each time a team me mber successfully crosses the gorge • The time si nce the simulation started that each team finishes at o You should maintain a global start time, so each team can compute their end time • The total number of racers to cross on each rope • The total execution time , this will the same as the time of the losing team.
For the output, make sure to clearly identify the Team and which Team Member . This can be done using integers (like done below) or you can name the Teams and Members using Strings.
Sample Output
The following output confirms correct simulation details in several ways. Total time is a little bit over 6.9 seconds. Team 3 is the loser, their cro ss time = total simulation time. The sum of the number of racers to cross the ropes adds up to 40, i.e. 20 racers in each direction. The number of racer s is well bala nced, i.e. about the same # use each rope.
Team 2 currently setting up gear for 233 ms Team 4 currently setting up gear for 291 ms Team 0 currently setting up gear for 297 ms Team 3 currently setting up gear for 270 ms Team 1 currently setting up gear for 318 ms Team 2, Member 3 crossed Forward on Rope #0 in 407 ms Team 2, Member 4 crossed Forward on Rope #1 in 527 ms Team 2, Member 1 crossed Forward on Rope #2 in 533 ms Team 0, Member 3 crossed Forward on Rope #0 in 443 ms Team 4, Member 4 crossed Forward on Rope #2 in 407 ms Team 3, Member 1 crossed Forward on Rope #1 in 432 ms Team 4, Member 2 crossed Forward on Rope #0 in 506 ms Team 3, Member 4 crossed Forward on Rope #1 in 459 ms Team 1, Member 2 crossed Forward on Rope #2 in 558 ms Team 0, Member 4 crossed Forward on Rope #0 in 507 ms Team 1, Member 4 crossed Forward on Rope #1 in 451 ms Team 2, Member 2 crossed Forward on Rope #2 in 507 ms Team 2 successfully obtained medallion Team 3, Member 3 crossed Forward on Rope #0 in 404 ms Team 0, Member 2 crossed Forward on Rope #1 in 513 ms Team 4, Membe r 1 crossed Forward on Rope #2 in 423 ms Team 0, Member 1 crossed Forward on Rope #0 in 405 ms Team 0 successfully obtained medallion Team 4, Member 3 crossed Forward on Rope #1 in 472 ms Team 4 successfully obtained medallion Team 2, Member 3 crossed B ack on Rope #2 in 471 ms Team 2, Member 4 crossed Back on Rope #0 in 553 ms Team 2, Member 1 crossed Back on Rope #2 in 472 ms Team 0, Member 2 crossed Back on Rope #1 in 561 ms Team 4, Member 2 crossed Back on Rope #2 in 423 ms Team 0, Member 3 cross ed Back on Rope #0 in 595 ms Team 1, Member 1 crossed Forward on Rope #1 in 561 ms Team 0, Member 1 crossed Back on Rope #2 in 591 ms Team 4, Member 1 crossed Back on Rope #0 in 587 ms Team 3, Member 2 crossed Forward on Rope #1 in 457 ms Team 3 successfully obtained medallion Team 2, Member 2 crossed Back on Rope #2 in 428 ms Team 2 completed crossing, total time since race started = 5.077 secs Team 4, Member 3 crossed Back on Rope # 0 in 506 ms Team 1, Member 3 crossed Forward on Rope #1 in 587 ms Team 1 successfully obtained medallion Team 0, Member 4 crossed Back on Rope #2 in 537 ms Team 0 completed crossing, total time since race started = 5.618 secs Team 4, Member 4 crossed Ba ck on Rope #0 in 587 ms Team 4 completed crossing, total time since race started = 5.77 secs Team 3, Member 2 crossed Back on Rope #1 in 587 ms Team 3, Member 4 crossed Back on Rope #2 in 506 ms Team 1, Member 4 crossed Back on Rope #0 in 541 ms Team 1, Member 2 crossed Back on Rope #1 in 494 ms Team 1, Member 1 crossed Back on Rope #2 in 436 ms Team 1, Member 3 crossed Back on Rope #0 in 455 ms Team 1 completed crossing, total time since race started = 6.771 secs Team 3, Member 1 crossed Back on Rop e #1 in 447 ms Team 3, Member 3 crossed Back on Rope #2 in 463 ms Team 3 completed crossing, total time since race started = 7.03 secs
The number of racers to cross Rope #0 was: 13 The number of racers to cross Rope #1 was: 13 The number of racers to cros s Rope #2 was: 14
Total Simulation time = 7.034 seconds
Submission
You will submit your java code in a zip folder to the su bmission folder on MyCanvas. There is no need to zip up the entire project, you just need to include all of the .java files in the zip folder .
Grading
You will be evaluated on the following: • Code Formatting and Documentation – 20% o Code is readable o Javadoc and commenting are complete ▪ No need to generate Javadoc in html files • Code Execution and Performance – 40% o Submission correctly solves the above problem ▪ Output very similar to samp le ▪ Appropriate delays are mea sured o timing and performance are good ▪ close to 6.9 seconds run time ▪ well ba lanced use of ropes • Code Structure – 40% o Code uses the correct programming concepts to solve the problem ▪ follows the UML design specification ▪ should use wait() / notify() and locks() , where appropriate ▪ penalty for using busy waiting ▪ penalty for accessing global variables without proper synchronization .