Question: POTENTIAL DATA RACES AND SYNCHRONISATION ISSUES package medleySimulation; import java.awt.Color; import java.util.Random; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Semaphore; public class Swimmer extends Thread { public

POTENTIAL DATA RACES AND SYNCHRONISATION ISSUES package medleySimulation;
import java.awt.Color;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
public class Swimmer extends Thread {
public static StadiumGrid stadium; // shared
private FinishCounter finish; // shared
private GridBlock currentBlock;
private Random rand;
private int movingSpeed;
private PeopleLocation myLocation;
private int ID; // thread ID
private int team; // team ID
private GridBlock start;
private CountDownLatch startLatch;
private Semaphore entranceSemaphore; // Semaphore for controlling entrance access
private boolean isBackstroke; // Flag to identify backstroke swimmers
private BlockingQueue backstrokeQueue; // Queue for backstroke synchronization
private CountDownLatch backstrokeLatch; // Latch to synchronize backstroke swimmers
public enum SwimStroke {
Backstroke(1,2.5, Color.black),
Breaststroke(2,2.1, new Color(255,102,0)),
Butterfly(3,2.55, Color.magenta),
Freestyle(4,2.8, Color.red);
private final double strokeTime;
private final int order; // in minutes
private final Color colour;
SwimStroke(int order, double sT, Color c){
this.strokeTime = sT;
this.order = order;
this.colour = c;
}
public int getOrder(){
return order;
}
public Color getColour(){
return colour;
}
}
private final SwimStroke swimStroke;
Swimmer(int ID, int t, PeopleLocation loc, FinishCounter f, int speed, SwimStroke s, CountDownLatch startLatch, Semaphore entranceSemaphore, BlockingQueue backstrokeQueue, CountDownLatch backstrokeLatch){
this.swimStroke = s;
this.ID = ID;
this.movingSpeed = speed;
this.myLocation = loc;
this.team = t;
this.start = stadium.returnStartingBlock(team);
this.finish = f;
this.rand = new Random();
this.startLatch = startLatch;
this.entranceSemaphore = entranceSemaphore;
this.isBackstroke =(s == SwimStroke.Backstroke);
this.backstrokeQueue = backstrokeQueue;
this.backstrokeLatch = backstrokeLatch;
}
public int getX(){
return currentBlock.getX();
}
public int getY(){
return currentBlock.getY();
}
public int getSpeed(){
return movingSpeed;
}
public SwimStroke getSwimStroke(){
return swimStroke;
}
public void enterStadium() throws InterruptedException {
System.out.println("Swimmer "+ ID +" trying to enter the stadium.");
entranceSemaphore.acquire(); // Acquire semaphore to enter
try {
currentBlock = stadium.enterStadium(myLocation);
System.out.println("Swimmer "+ ID +" has entered the stadium.");
sleep(200); // Wait a bit at the door, look around
} finally {
entranceSemaphore.release(); // Release semaphore after entering
}
}
public void goToStartingBlocks() throws InterruptedException {
int x_st = start.getX();
int y_st = start.getY();
// Move to the starting blocks
System.out.println("Swimmer "+ ID +" moving to starting blocks at ("+ x_st +","+ y_st +").");
while (currentBlock != start){
sleep(movingSpeed *3); // Not rushing
currentBlock = stadium.moveTowards(currentBlock, x_st, y_st, myLocation);
}
// Wait until all backstroke swimmers are at their starting blocks
if (isBackstroke){
System.out.println("Backstroke swimmer "+ ID +" has reached the starting block.");
backstrokeQueue.put(this); // Add to queue
backstrokeLatch.countDown(); // Decrement the latch
System.out.println("Backstroke swimmer "+ ID +" added to queue. Countdown latch count: "+ backstrokeLatch.getCount());
backstrokeLatch.await(); // Wait for all backstroke swimmers to be ready
System.out.println("Backstroke swimmer "+ ID +" is now ready.");
} else {
System.out.println(swimStroke +" swimmer "+ ID +" waiting for backstroke swimmers.");
backstrokeLatch.await(); // Ensure non-backstroke swimmers wait for backstroke swimmers
System.out.println(swimStroke +" swimmer "+ ID +" is now ready.");
}
}
private void dive() throws InterruptedException {
int x = currentBlock.getX();
int y = currentBlock.getY();
System.out.println("Swimmer "+ ID +" diving from ("+ x +","+ y +").");
currentBlock = stadium.jumpTo(currentBlock, x, y -2, myLocation);
}
private void swimRace() throws Interrupte

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!