This commit is contained in:
Zach S-B 2021-09-28 19:10:27 +10:00
parent 0ca9f524ec
commit 828a66632a
2 changed files with 93 additions and 37 deletions

View File

@ -1,5 +1,6 @@
import java.util.*; import java.util.*;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import java.io.*; import java.io.*;
public class P1 { public class P1 {
@ -11,8 +12,13 @@ public class P1 {
* Student Number: c3262201 * Student Number: c3262201
*/ */
LinkedList<Thread> WARs = new LinkedList<Thread>(); LinkedList<P1_War> WARs = new LinkedList<P1_War>();
LinkedList<Thread> threads = new LinkedList<Thread>();
Semaphore intersection = new Semaphore(1, true); Semaphore intersection = new Semaphore(1, true);
int[] tracks = new int[2];
/** /**
* Main. Runs the program * Main. Runs the program
*/ */
@ -23,8 +29,16 @@ public class P1 {
public void run() public void run()
{ {
tracks[0]=0;
tracks[1]=0;
readFile("../P1-1in.txt"); 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();
}
} }
/** /**
@ -56,7 +70,8 @@ public class P1 {
for(int i = 0; i< inputID; i++) 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++; warNum++;
} }
} }
@ -67,7 +82,7 @@ public class P1 {
for(int i = 0; i< inputID; i++) 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++; warNum++;
} }
} }
@ -78,7 +93,7 @@ public class P1 {
for(int i = 0; i< inputID; i++) 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++; warNum++;
} }
} }
@ -89,7 +104,7 @@ public class P1 {
for(int i = 0; i< inputID; i++) 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++; warNum++;
} }
} }

View File

@ -6,6 +6,8 @@ public class P1_War implements Runnable{
private int id; //ID number of the WAR private int id; //ID number of the WAR
private boolean atDock; //Is the WAR at the dock? True if at dock 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? private int trackNum; //Which track does the WAR operate in? 1 or 2?
boolean finished = false;
int[] tracks;
/** /**
* WAR (Wharehouse Assistance Robot) object * WAR (Wharehouse Assistance Robot) object
@ -13,46 +15,80 @@ public class P1_War implements Runnable{
* @param trackNumInput The track that the WAR is on * @param trackNumInput The track that the WAR is on
* @param atDockInput Is the WAR at the dock (True) or at Storage (False) * @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; id = idInput;
atDock = atDockInput; atDock = atDockInput;
trackNum = trackNumInput; trackNum = trackNumInput;
this.sem=intersection; this.sem=intersection;
tracks = tracksInput;
} }
public void run() public void run()
{ {
String loadedState; String loadedState;
if(atDock) String destination;
{
loadedState = " (Loaded) ";
}
else
{
loadedState = " (Unloaded) ";
}
System.out.println("WAR-"+id+loadedState+"Waiting at the Intersection. Going towards Storage"+trackNum); while(!finished)
//aquire semaphore
sem.acquireUninterruptibly();
for(int i = 1; i<3; i++)
{ {
System.out.println("WAR-"+id+loadedState+"Crossing intersection Checkpoint "+i+"."); // if(tracks[0]>=150 && tracks[1] >=150)
try { // {
Thread.sleep(200); // finished=true;
} catch (InterruptedException e) { // break;
// Don't care // }
//e.printStackTrace();
//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();
} }
} }
System.out.println("WAR-"+id+loadedState+"Crossed the intersection.");
sem.release();
//Change location to the otherside of the intersection
atDock=!atDock;
} }
//Getters //Getters
@ -77,4 +113,9 @@ public class P1_War implements Runnable{
atDock=atDockInput; atDock=atDockInput;
} }
public void setFinished(boolean input)
{
finished = input;
}
} }