From 0ca9f524ec9dacd055fa043d9d2a82e324dd5bf0 Mon Sep 17 00:00:00 2001 From: Zach Date: Tue, 28 Sep 2021 17:11:58 +1000 Subject: [PATCH] Pre-thread more change --- Code/P1_War.java | 77 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 69 insertions(+), 8 deletions(-) diff --git a/Code/P1_War.java b/Code/P1_War.java index 1a9f925..5b36c02 100644 --- a/Code/P1_War.java +++ b/Code/P1_War.java @@ -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; } } \ No newline at end of file