import java.util.concurrent.*; public class P1_War implements Runnable{ Semaphore sem; private int id; //ID number of the WAR 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? /** * 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; 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; } }