From 828a66632af64b4e9cc283b5a418f737929bf4a4 Mon Sep 17 00:00:00 2001 From: Zach Date: Tue, 28 Sep 2021 19:10:27 +1000 Subject: [PATCH] Working --- Code/P1.java | 29 ++++++++++---- Code/P1_War.java | 101 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 93 insertions(+), 37 deletions(-) diff --git a/Code/P1.java b/Code/P1.java index e9fefa8..aa0e4bb 100644 --- a/Code/P1.java +++ b/Code/P1.java @@ -1,5 +1,6 @@ import java.util.*; import java.util.concurrent.Semaphore; + import java.io.*; public class P1 { @@ -11,8 +12,13 @@ public class P1 { * Student Number: c3262201 */ - LinkedList WARs = new LinkedList(); + LinkedList WARs = new LinkedList(); + LinkedList threads = new LinkedList(); Semaphore intersection = new Semaphore(1, true); + + int[] tracks = new int[2]; + + /** * Main. Runs the program */ @@ -23,8 +29,16 @@ public class P1 { public void run() { + tracks[0]=0; + tracks[1]=0; + readFile("../P1-1in.txt"); - printState(); + //Create thread for each war + for(int i = 0; i < WARs.size(); i++) + { + threads.add(new Thread(WARs.get(i))); + threads.get(i).start(); + } } /** @@ -55,8 +69,9 @@ public class P1 { for(int i = 0; i< inputID; i++) { - - //WARs.add(new P1_War(warNum, 2, false, intersection)); + + WARs.add(new P1_War(warNum, 2, false, intersection, tracks)); + //WARs.add(new Thread(new P1_War(warNum, 2, false, intersection))); warNum++; } } @@ -67,7 +82,7 @@ public class P1 { for(int i = 0; i< inputID; i++) { - WARs.add(new P1_War(warNum, 2, true, intersection)); + WARs.add(new P1_War(warNum, 2, true, intersection, tracks)); warNum++; } } @@ -78,7 +93,7 @@ public class P1 { for(int i = 0; i< inputID; i++) { - WARs.add(new P1_War(warNum, 1, false, intersection)); + WARs.add(new P1_War(warNum, 1, false, intersection, tracks)); warNum++; } } @@ -89,7 +104,7 @@ public class P1 { for(int i = 0; i< inputID; i++) { - WARs.add(new P1_War(warNum, 1, true, intersection)); + WARs.add(new P1_War(warNum, 1, true, intersection, tracks)); warNum++; } } diff --git a/Code/P1_War.java b/Code/P1_War.java index 5b36c02..0ea7e1e 100644 --- a/Code/P1_War.java +++ b/Code/P1_War.java @@ -6,6 +6,8 @@ public class P1_War implements Runnable{ 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? + boolean finished = false; + int[] tracks; /** * WAR (Wharehouse Assistance Robot) object @@ -13,48 +15,82 @@ public class P1_War implements Runnable{ * @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) { + public P1_War(int idInput, int trackNumInput, boolean atDockInput, Semaphore intersection,int[] tracksInput) { id = idInput; atDock = atDockInput; trackNum = trackNumInput; this.sem=intersection; + tracks = tracksInput; } public void run() { String loadedState; - if(atDock) + String destination; + + while(!finished) { - loadedState = " (Loaded) "; + // if(tracks[0]>=150 && tracks[1] >=150) + // { + // finished=true; + // break; + // } + + //if(!finished) + { + if(atDock) + { + loadedState = " (Loaded) "; + destination = "Dock"; + } + else + { + loadedState = " (Unloaded) "; + destination = "Storage"; + } + + if(!(tracks[0]>=150 && tracks[1] >=150)) + { + System.out.println("WAR-"+id+loadedState+"Waiting at the Intersection. Going towards "+destination+trackNum); + } + + //aquire semaphore + sem.acquireUninterruptibly(); + if(tracks[0]>=150 && tracks[1] >=150) + { + finished=true; + } + if(!finished) + { + for(int i = 1; i<=3; i++) + { + System.out.println("WAR-"+id+loadedState+"Crossing intersection Checkpoint "+i+"."); + try { + Thread.sleep(2); + } catch (InterruptedException e) { + // Don't care + //e.printStackTrace(); + } + } + + System.out.println("WAR-"+id+loadedState+"Crossed the intersection."); + atDock=!atDock; + + if(trackNum == 1) + { + tracks[0]++; + } + else if (trackNum == 2) + { + tracks[1]++; + } + System.out.println("Total crossed in Track1 "+tracks[0]+" Track2 "+tracks[1]); + } + sem.release(); + } } - 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() { @@ -77,4 +113,9 @@ public class P1_War implements Runnable{ atDock=atDockInput; } + public void setFinished(boolean input) + { + finished = input; + } + } \ No newline at end of file