Working head queue thing
This commit is contained in:
parent
ddd143b525
commit
b9c9ff2f79
@ -20,12 +20,7 @@ public class P2_Jobs implements Runnable{
|
|||||||
|
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
int head=0;
|
printer.requestPrint(this,printer.requestHead());
|
||||||
do
|
|
||||||
{
|
|
||||||
//While the job hasn't been started, keep nagging
|
|
||||||
head = printer.requestHead();
|
|
||||||
}while(!printer.requestPrint(this,head));
|
|
||||||
|
|
||||||
//Once we're out of that while loop, we're done.
|
//Once we're out of that while loop, we're done.
|
||||||
done = true;
|
done = true;
|
||||||
|
|||||||
@ -1,73 +1,46 @@
|
|||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
public class P2_Printer{
|
public class P2_Printer{
|
||||||
|
|
||||||
private int[] time = {0}; //Really hacky solution, but seems easier than wrapping in a custom object.
|
private int[] time = {0}; //Really hacky solution, but seems easier than wrapping in a custom object.
|
||||||
private char printerMode = 'M';
|
private char printerMode = 'M';
|
||||||
|
|
||||||
private P2_PrinterHead p1 = new P2_PrinterHead();
|
private LinkedList<P2_PrinterHead> printerHeadsWaiting = new LinkedList<P2_PrinterHead>();
|
||||||
private P2_PrinterHead p2 = new P2_PrinterHead();
|
|
||||||
private P2_PrinterHead p3 = new P2_PrinterHead();
|
|
||||||
|
|
||||||
|
private P2_PrinterHead p1 = new P2_PrinterHead(1);
|
||||||
|
private P2_PrinterHead p2 = new P2_PrinterHead(2);
|
||||||
|
private P2_PrinterHead p3 = new P2_PrinterHead(3);
|
||||||
|
|
||||||
public P2_Printer()
|
public P2_Printer()
|
||||||
{
|
{
|
||||||
//Nothing to set up.
|
//Nothing to set up.
|
||||||
|
printerHeadsWaiting.add(p1);
|
||||||
|
printerHeadsWaiting.add(p2);
|
||||||
|
printerHeadsWaiting.add(p3);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized int requestHead()
|
public synchronized P2_PrinterHead requestHead()
|
||||||
{
|
{
|
||||||
int head = 1;
|
while(printerHeadsWaiting.size()==0)
|
||||||
int shortestTimeRemaining = p1.getTimeRemaining();
|
|
||||||
|
|
||||||
if(shortestTimeRemaining > p2.getTimeRemaining())
|
|
||||||
{
|
{
|
||||||
shortestTimeRemaining = p2.getTimeRemaining();
|
//If there isn't a head available, pause for a moment and check again.
|
||||||
head = 2;
|
try
|
||||||
|
{
|
||||||
|
Thread.sleep(0);
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
//Don't caare
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return printerHeadsWaiting.removeFirst();
|
||||||
if(shortestTimeRemaining > p3.getTimeRemaining())
|
|
||||||
{
|
|
||||||
head = 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
return head;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean requestPrint(P2_Jobs jobInput, int headInput)
|
public boolean requestPrint(P2_Jobs jobInput, P2_PrinterHead printHeadToUse)
|
||||||
{
|
{
|
||||||
//Check if job can be done currently
|
printHeadToUse.print(jobInput, time);
|
||||||
if(jobInput.getJobType()==printerMode) //If the job matches the current printer mode
|
printerHeadsWaiting.add(printHeadToUse);
|
||||||
{
|
return true;
|
||||||
//If there is an available head
|
|
||||||
if(headInput==1)
|
|
||||||
{
|
|
||||||
//Send to a head to be printed and count the time
|
|
||||||
int startTime = p1.print(jobInput, time);
|
|
||||||
System.out.println("("+startTime+") "+jobInput.getId()+" uses head 1 (time: "+jobInput.getPages()+")");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if(headInput==2)
|
|
||||||
{
|
|
||||||
int startTime = p2.print(jobInput, time);
|
|
||||||
System.out.println("("+startTime+") "+jobInput.getId()+" uses head 2 (time: "+jobInput.getPages()+")");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if(headInput==3)
|
|
||||||
{
|
|
||||||
int startTime = p3.print(jobInput, time);
|
|
||||||
System.out.println("("+startTime+") "+jobInput.getId()+" uses head 3 (time: "+jobInput.getPages()+")");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//Keep waiting and asking
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,22 +1,22 @@
|
|||||||
public class P2_PrinterHead{
|
public class P2_PrinterHead{
|
||||||
|
|
||||||
private char mode = 'M';
|
private char mode = 'M';
|
||||||
private boolean inUse=false;
|
|
||||||
private String jobId;
|
private String jobId;
|
||||||
private int timeRemaining = 0;
|
private int timeRemaining = 0;
|
||||||
|
|
||||||
public P2_PrinterHead()
|
private int id;
|
||||||
|
|
||||||
|
public P2_PrinterHead(int idInput)
|
||||||
{
|
{
|
||||||
//Nothing to set up.
|
//Nothing to set up.
|
||||||
|
id = idInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized int print(P2_Jobs jobInput, int[] time)
|
public synchronized void print(P2_Jobs jobInput, int[] time)
|
||||||
{
|
{
|
||||||
//Set the details of the current job
|
//Set the details of the current job
|
||||||
jobId = jobInput.getId();
|
jobId = jobInput.getId();
|
||||||
mode = jobInput.getJobType();
|
mode = jobInput.getJobType();
|
||||||
inUse = true;
|
|
||||||
timeRemaining = jobInput.getPages();
|
|
||||||
int startTime = time[0];
|
int startTime = time[0];
|
||||||
|
|
||||||
//Wait (and delay the thread) one second per page of the job
|
//Wait (and delay the thread) one second per page of the job
|
||||||
@ -25,7 +25,6 @@ public class P2_PrinterHead{
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
timeRemaining--;
|
|
||||||
time[0]++;
|
time[0]++;
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch(Exception e)
|
||||||
@ -33,23 +32,21 @@ public class P2_PrinterHead{
|
|||||||
//Don't actually care
|
//Don't actually care
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
System.out.println("("+startTime+") "+jobInput.getId()+" uses head "+getId()+"(time: "+jobInput.getPages()+")");
|
||||||
//Head is now available for another activity
|
|
||||||
inUse = false;
|
|
||||||
return startTime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Getters
|
//Getters
|
||||||
|
|
||||||
|
public int getId()
|
||||||
|
{
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
public String getJobId()
|
public String getJobId()
|
||||||
{
|
{
|
||||||
return jobId;
|
return jobId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getInUse()
|
|
||||||
{
|
|
||||||
return inUse;
|
|
||||||
}
|
|
||||||
|
|
||||||
public char getMode()
|
public char getMode()
|
||||||
{
|
{
|
||||||
return mode;
|
return mode;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user