This is my code. As you see in the run method, I assign values to tStart, tEnd, tAround and wTime. But when the Thread ends, they still have the default values of -1. I try printing out their values while the run() is running, and I have the correct values. But they are not 'writing' those values back to the variables when the thread ends.
public class PCB extends Thread{
public int id, arrivalTime, cpuBurst, ioBurst;
public int tStart, tEnd, tAround, wTime;
public PCB(){
id = -1;
arrivalTime = -1;
cpuBurst = -1;
ioBurst = -1;
tStart = -1;
tEnd = -1;
tAround = -1;
wTime = -1;
}
public void run(){
try{
.........
//calculation for FCFS
if (id == 1){ //special case for the first one
tStart = arrivalTime;
}
else tStart = lastEndTime;
tEnd = tStart + cpuBurst + ioBurst;
tAround = tEnd - arrivalTime;
wTime = tStart - arrivalTime;
PCBThreadStopFlag = true;
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
When the thread ends, this is how I print out the values:
// now we print out the process table
String format = "|P%1$-10s|%2$-10s|%3$-10s|%4$-10s|%5$-10s|%6$-10s|%7$-10s|%8$-10s|\n";
System.out.format(format, "ID", "ArrTime", "CPUBurst", "I/OBurst", "TimeStart", "TimeEnd","TurnAround","WaitTime");
ListIterator<PCB> iter = resultQueue.listIterator();
while(iter.hasNext()){
PCB temp = iter.next();
System.out.format(format, temp.id, temp.arrivalTime, temp.cpuBurst, temp.ioBurst, temp.tStart, temp.tEnd, temp.tAround, temp.wTime );
}
And here is how I waited for the thread to stop first:
while(!rq.values.isEmpty()){
//System.out.println("Ready queue capacity now: " + rq.values.size());
currentProcess = new PCB(rq.values.getFirst());
currentProcess.start();
while(PCBThreadStopFlag == false) {}
//currentProcess.stop();
PCBThreadStopFlag = false;
//after everything is done, remove the first pcb
// and add it to the result queue (just to print the report)
resultQueue.addLast(rq.values.removeFirst());
}
I use the flag PCBThreadStopFlag in the run() up top (at the end when all the assignments are done) then in this function, I use while(PCBThreadStopFlag == false) {} to do the "busy-wait" task. may be this is the cause??