Question: In this assignment, your objective is to correct the existing code for a multithreaded Java simulation of a 4 x 1 0 0 medley relay
In this assignment, your objective is to correct the existing code for a
multithreaded Java simulation of a x medley relay swimming race. You
must use synchronization mechanisms to ensure that the simulation
adheres to the specified synchronization constraints and maintains safety
and liveness. im currently stuck as my threads wont dive to the pool as the race must start when all threads are ready public class MedleySimulation
public static void mainString args throws InterruptedException
teams new SwimTeamnumTeams;
teamsi new SwimTeami finishLine, peopleLocations;
setupGUIframeX frameY; Setup GUI
Start viewer thread
Thread view new ThreadstadiumView;
view.start;
Start counter thread for updating results
Thread results new ThreadcounterDisplay;
results.start;
Start all team threads which will start swimmers
for int i ; i numTeams; i
teamsistart;
Notify swimmers that the race can start
Swimmer.startSignal.countDown; Signal the Start button clicked
Ensure synchronization if needed, such as waiting for all teams to complete
For example, waiting for all swimmers to finish if needed
Swimmer.raceStartLatch.await; Uncomment if you have additional synchronization
public class MedleySimulation
Main method starts it all
public static void mainString args throws InterruptedException
Initialize the stadium grid, finish line, and other components
int numbanes numTeams; Number of lanes should match the number of teams
Swimmer.initializeLaneStartLatchesnumLanes; Initialize laneStartLatches
int numberOfPermits ; Adjust the number of permits as needed
Swimmer.entranceSemaphore new SemaphorenumberOfPermits;
Swimmer.initializeStartSignal; Initialize the start signal
Swimmer.initializeRaceStartLatchnumTeams; Initialize the race start latch
finishLine new FinishCounter; Initialize counters
stadiumGrid new StadiumGridgridX gridY, numTeams, finishLine; Setup stadium with size
SwimTeam.stadium stadiumGrid; Set grid shared with SwimTeam
Swimmer.stadium stadiumGrid; Set grid shared with Swimmer
peopleLocations new PeopleLocationnumTeams SwimTeam.sizeOfTeam; Four swimmers per team
teams new SwimTeamnumTeams;
for int ; numTeams;
setupGUIframeX frameY; Setup GUI
Start viewer thread
Thread view new ThreadstadiumView;
view.start;
Start counter thread for updating results
Thread results new ThreadcounterDisplay;
results.start;
Start all team threads which will start swimmers
for int ; numTeams; Go to the starting blocks
public void goToStartingBlocks throws InterruptedException usage
int xst start.getX;
int yst start.getY;
while currentBlock start
sleep millis: movingSpeed ; Not rushing
currentBlock stadium.moveTowardscurrentBlock xst yst myLocation;
System.out.printlnThread
this.ID at start currentBlock.getX currentBlock.getY;
Print latch count before awaiting
System.out.printlnLatch count before await: ZaneStartLatchesteamgetCount;
Await the start signal
ZaneStartLatchesteamawait ;
ZaneStartLatchesteam countDown;
Notify that the swimmer is ready
System.out.printlnSwimmer ID notified that the race can start.";
Print latch count after await
System.out.printlnLatch count after await: ZaneStartLatchesteamgetCount;
Decrement the latch to signal readiness
Dive into the pool
private void dive throws InterruptedException usage
int currentBlock.getX;
int currentBlock.getY;
currentBlock stadium.jumpTocurrentBlock: myLocation;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
