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.concurrent.Semaphore;
import java.io.*;
public class P1 {
@ -11,8 +12,13 @@ public class P1 {
* 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);
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();
}
}
/**
@ -56,7 +70,8 @@ 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++;
}
}

View File

@ -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,35 +15,58 @@ 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;
String destination;
while(!finished)
{
// if(tracks[0]>=150 && tracks[1] >=150)
// {
// finished=true;
// break;
// }
//if(!finished)
{
if(atDock)
{
loadedState = " (Loaded) ";
destination = "Dock";
}
else
{
loadedState = " (Unloaded) ";
destination = "Storage";
}
System.out.println("WAR-"+id+loadedState+"Waiting at the Intersection. Going towards Storage"+trackNum);
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();
for(int i = 1; i<3; i++)
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(200);
Thread.sleep(2);
} catch (InterruptedException e) {
// Don't care
//e.printStackTrace();
@ -49,10 +74,21 @@ public class P1_War implements Runnable{
}
System.out.println("WAR-"+id+loadedState+"Crossed the intersection.");
sem.release();
//Change location to the otherside of 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();
}
}
}
//Getters
@ -77,4 +113,9 @@ public class P1_War implements Runnable{
atDock=atDockInput;
}
public void setFinished(boolean input)
{
finished = input;
}
}