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 4x100 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 main(String[] args) throws InterruptedException {
teams = new SwimTeam[numTeams];
teams[i]= new SwimTeam(i, finishLine, peopleLocations);
}
setupGUI(frameX, frameY); // Setup GUI
// Start viewer thread
Thread view = new Thread(stadiumView);
view.start();
// Start counter thread - for updating results
Thread results = new Thread(counterDisplay);
results.start();
// Start all team threads (which will start swimmers)
for (int i =0; i numTeams; i++){
teams[i].start();
}
// 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 main(String[] 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.initializeLaneStartLatches(numLanes); // Initialize laneStartLatches
int numberOfPermits =1; // Adjust the number of permits as needed
Swimmer.entranceSemaphore = new Semaphore(numberOfPermits);
Swimmer.initializeStartSignal(); // Initialize the start signal
Swimmer.initializeRaceStartLatch(numTeams); // Initialize the race start latch
finishLine = new FinishCounter(); // Initialize counters
stadiumGrid = new StadiumGrid(gridX, 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 PeopleLocation[numTeams * SwimTeam.sizeOfTeam]; // Four swimmers per team
teams = new SwimTeam[numTeams];
for (int i?=0;i? numTeams; i++){
}
setupGUI(frameX, frameY); // Setup GUI
// Start viewer thread
Thread view = new Thread(stadiumView);
view.start();
// Start counter thread - for updating results
Thread results = new Thread(counterDisplay);
results.start();
// Start all team threads (which will start swimmers)
for (int i?=0;i? numTeams; i++){// Go to the starting blocks
public void goToStartingBlocks() throws InterruptedException {1 usage
int x_st = start.getX();
int y_st = start.getY();
while (currentBlock != start){
sleep ( millis: movingSpeed *3) ; // Not rushing
currentBlock = stadium.moveTowards(currentBlock, x_st, y_st, myLocation);
}
System.out.println("-----------Thread "+
this.ID +" at start "+ currentBlock.getX()+""+ currentBlock.getY());
// Print latch count before awaiting
System.out.println("Latch count before await: "+ ZaneStartLatches[team].getCount());
// Await the start signal
ZaneStartLatches[team].await() ;
ZaneStartLatches[team]. countDown();
// Notify that the swimmer is ready
System.out.println("Swimmer "+ ID +" notified that the race can start.");
// Print latch count after await
System.out.println("Latch count after await: "+ ZaneStartLatches[team].getCount());
// Decrement the latch to signal readiness
}
// Dive into the pool
private void dive() throws InterruptedException {1 usage
int x= currentBlock.getX();
int y= currentBlock.getY();
currentBlock = stadium.jumpTo(currentBlock,x,y:y-2, myLocation);
}
In this assignment, your objective is to correct

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!