Working
This commit is contained in:
parent
0ca9f524ec
commit
828a66632a
27
Code/P1.java
27
Code/P1.java
@ -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++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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,35 +15,58 @@ 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;
|
||||||
|
String destination;
|
||||||
|
|
||||||
|
while(!finished)
|
||||||
|
{
|
||||||
|
// if(tracks[0]>=150 && tracks[1] >=150)
|
||||||
|
// {
|
||||||
|
// finished=true;
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
|
||||||
|
//if(!finished)
|
||||||
|
{
|
||||||
if(atDock)
|
if(atDock)
|
||||||
{
|
{
|
||||||
loadedState = " (Loaded) ";
|
loadedState = " (Loaded) ";
|
||||||
|
destination = "Dock";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
loadedState = " (Unloaded) ";
|
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
|
//aquire semaphore
|
||||||
sem.acquireUninterruptibly();
|
sem.acquireUninterruptibly();
|
||||||
|
if(tracks[0]>=150 && tracks[1] >=150)
|
||||||
for(int i = 1; i<3; i++)
|
{
|
||||||
|
finished=true;
|
||||||
|
}
|
||||||
|
if(!finished)
|
||||||
|
{
|
||||||
|
for(int i = 1; i<=3; i++)
|
||||||
{
|
{
|
||||||
System.out.println("WAR-"+id+loadedState+"Crossing intersection Checkpoint "+i+".");
|
System.out.println("WAR-"+id+loadedState+"Crossing intersection Checkpoint "+i+".");
|
||||||
try {
|
try {
|
||||||
Thread.sleep(200);
|
Thread.sleep(2);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// Don't care
|
// Don't care
|
||||||
//e.printStackTrace();
|
//e.printStackTrace();
|
||||||
@ -49,10 +74,21 @@ public class P1_War implements Runnable{
|
|||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("WAR-"+id+loadedState+"Crossed the intersection.");
|
System.out.println("WAR-"+id+loadedState+"Crossed the intersection.");
|
||||||
|
|
||||||
sem.release();
|
|
||||||
//Change location to the otherside of the intersection
|
|
||||||
atDock=!atDock;
|
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
|
//Getters
|
||||||
@ -77,4 +113,9 @@ public class P1_War implements Runnable{
|
|||||||
atDock=atDockInput;
|
atDock=atDockInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setFinished(boolean input)
|
||||||
|
{
|
||||||
|
finished = input;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user