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 Color.black
Breaststroke new Color
Butterfly Color.magenta
Freestyle Color.red;
private final double strokeTime;
private final int order; in minutes
private final Color colour;
SwimStrokeint 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;
Swimmerint 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.returnStartingBlockteam;
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.printlnSwimmer ID trying to enter the stadium.";
entranceSemaphore.acquire; Acquire semaphore to enter
try
currentBlock stadium.enterStadiummyLocation;
System.out.printlnSwimmer ID has entered the stadium.";
sleep; Wait a bit at the door, look around
finally
entranceSemaphore.release; Release semaphore after entering
public void goToStartingBlocks throws InterruptedException
int xst start.getX;
int yst start.getY;
Move to the starting blocks
System.out.printlnSwimmer ID moving to starting blocks at xst yst ;
while currentBlock start
sleepmovingSpeed ; Not rushing
currentBlock stadium.moveTowardscurrentBlock xst yst myLocation;
Wait until all backstroke swimmers are at their starting blocks
if isBackstroke
System.out.printlnBackstroke swimmer ID has reached the starting block.";
backstrokeQueue.putthis; Add to queue
backstrokeLatch.countDown; Decrement the latch
System.out.printlnBackstroke swimmer ID added to queue. Countdown latch count: backstrokeLatch.getCount;
backstrokeLatch.await; Wait for all backstroke swimmers to be ready
System.out.printlnBackstroke swimmer ID is now ready.";
else
System.out.printlnswimStroke swimmer ID waiting for backstroke swimmers.";
backstrokeLatch.await; Ensure nonbackstroke swimmers wait for backstroke swimmers
System.out.printlnswimStroke swimmer ID is now ready.";
private void dive throws InterruptedException
int x currentBlock.getX;
int y currentBlock.getY;
System.out.printlnSwimmer ID diving from x y ;
currentBlock stadium.jumpTocurrentBlock x y myLocation;
private void swimRace throws Interrupte
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
