Working head queue thing

This commit is contained in:
Zach S-B 2021-10-01 11:14:04 +10:00
parent ddd143b525
commit b9c9ff2f79
3 changed files with 38 additions and 73 deletions

View File

@ -20,12 +20,7 @@ public class P2_Jobs implements Runnable{
public void run()
{
int head=0;
do
{
//While the job hasn't been started, keep nagging
head = printer.requestHead();
}while(!printer.requestPrint(this,head));
printer.requestPrint(this,printer.requestHead());
//Once we're out of that while loop, we're done.
done = true;

View File

@ -1,73 +1,46 @@
import java.util.LinkedList;
public class P2_Printer{
private int[] time = {0}; //Really hacky solution, but seems easier than wrapping in a custom object.
private char printerMode = 'M';
private P2_PrinterHead p1 = new P2_PrinterHead();
private P2_PrinterHead p2 = new P2_PrinterHead();
private P2_PrinterHead p3 = new P2_PrinterHead();
private LinkedList<P2_PrinterHead> printerHeadsWaiting = new LinkedList<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()
{
//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;
int shortestTimeRemaining = p1.getTimeRemaining();
if(shortestTimeRemaining > p2.getTimeRemaining())
while(printerHeadsWaiting.size()==0)
{
shortestTimeRemaining = p2.getTimeRemaining();
head = 2;
//If there isn't a head available, pause for a moment and check again.
try
{
Thread.sleep(0);
}
catch(Exception e)
{
//Don't caare
}
}
if(shortestTimeRemaining > p3.getTimeRemaining())
{
head = 3;
}
return head;
return printerHeadsWaiting.removeFirst();
}
public boolean requestPrint(P2_Jobs jobInput, int headInput)
public boolean requestPrint(P2_Jobs jobInput, P2_PrinterHead printHeadToUse)
{
//Check if job can be done currently
if(jobInput.getJobType()==printerMode) //If the job matches the current printer mode
{
//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;
}
printHeadToUse.print(jobInput, time);
printerHeadsWaiting.add(printHeadToUse);
return true;
}
}

View File

@ -1,22 +1,22 @@
public class P2_PrinterHead{
private char mode = 'M';
private boolean inUse=false;
private String jobId;
private int timeRemaining = 0;
public P2_PrinterHead()
private int id;
public P2_PrinterHead(int idInput)
{
//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
jobId = jobInput.getId();
mode = jobInput.getJobType();
inUse = true;
timeRemaining = jobInput.getPages();
int startTime = time[0];
//Wait (and delay the thread) one second per page of the job
@ -25,7 +25,6 @@ public class P2_PrinterHead{
try
{
Thread.sleep(1000);
timeRemaining--;
time[0]++;
}
catch(Exception e)
@ -33,23 +32,21 @@ public class P2_PrinterHead{
//Don't actually care
}
}
//Head is now available for another activity
inUse = false;
return startTime;
System.out.println("("+startTime+") "+jobInput.getId()+" uses head "+getId()+"(time: "+jobInput.getPages()+")");
}
//Getters
public int getId()
{
return id;
}
public String getJobId()
{
return jobId;
}
public boolean getInUse()
{
return inUse;
}
public char getMode()
{
return mode;