Pre-thread more change

This commit is contained in:
Zach S-B 2021-09-28 17:11:58 +10:00
parent 425aee20ca
commit 0ca9f524ec

View File

@ -1,19 +1,80 @@
public class P1_War {
import java.util.concurrent.*;
public class P1_War implements Runnable{
Semaphore sem;
private int id; //ID number of the WAR
private boolean inIntersection; //Is the WAR in the intersuction? True if in intersection
private boolean atDock; //Is the WAR at the dock? True if at dock
private int trackNum; //Which track does the WAR operate in? 1 or 2?
//Constructors
public P1_War(){
}
public P1_War(int idInput, int trackNumInput, boolean atDockInput, boolean inIntersectionInput) {
/**
* WAR (Wharehouse Assistance Robot) object
* @param idInput ID number for the WAR
* @param trackNumInput The track that the WAR is on
* @param atDockInput Is the WAR at the dock (True) or at Storage (False)
*/
public P1_War(int idInput, int trackNumInput, boolean atDockInput, Semaphore intersection) {
id = idInput;
inIntersection = inIntersectionInput;
atDock = atDockInput;
trackNum = trackNumInput;
this.sem=intersection;
}
public void run()
{
String loadedState;
if(atDock)
{
loadedState = " (Loaded) ";
}
else
{
loadedState = " (Unloaded) ";
}
System.out.println("WAR-"+id+loadedState+"Waiting at the Intersection. Going towards Storage"+trackNum);
//aquire semaphore
sem.acquireUninterruptibly();
for(int i = 1; i<3; i++)
{
System.out.println("WAR-"+id+loadedState+"Crossing intersection Checkpoint "+i+".");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// Don't care
//e.printStackTrace();
}
}
System.out.println("WAR-"+id+loadedState+"Crossed the intersection.");
sem.release();
//Change location to the otherside of the intersection
atDock=!atDock;
}
//Getters
public int getID()
{
return id;
}
public boolean getAtDock()
{
return atDock;
}
public int getTrackNum()
{
return trackNum;
}
//Setters
public void setAtDock(boolean atDockInput)
{
atDock=atDockInput;
}
}