I am trying to write a simple CPU scheduler. (It was, but is no longer homework, I am just trying to understand it.)
As part of this I am adding objects to an ArrayList, and then moving it to a different ArrayList. However, I am running into two problems:
- Each time an object is added to the ArrayList, it overwrites all previous objects with the new values (along with adding the new object).
- As soon as the loop calling the method to create/add objects to the ArrayList ends, the ArrayList is empty of objects, despite being declared static.
I have looked at the ArrayList APIs and done searching on stackoverflow but have not found a useful solution.
Relevant code follows (I've trimmed everything that I don't think is relevant, and there are several placeholder variables as this is mid-completion):
public class Tasks {
public static ArrayList<PCB>jobQueue = new ArrayList<>();
public static ArrayList<PCB>readyQueue = new ArrayList<>(3);
public ArrayList<Integer>tempCPU = new ArrayList<>();
public ArrayList<Integer>tempIO = new ArrayList<>();
public int nextDigit, a, b, c, d, e, f = 0;
public int jobCount = 0;
//method to read in text from text file (trimmed)
public void fileInput(String filename){
while (fileInput.hasNextLine()){
String line = fileInput.nextLine();
numberInput(line);
}
}
//method to parse the input data, create a PCB class object using it,
//and add it to the jobQueue ArrayList
public void numberInput(String line){
//Add PCB object (job) to jobQueue ArrayList
jobQueue.add(new PCB(tempCPU, tempIO, jobCount, jobCount, c, d, e, f));
//clear temporary ArrayLists (filled earlier in
//the method with now-trimmed code)
tempCPU.clear();
tempIO.clear();
}
}//end Tasks class
public class PCB {
ArrayList<Integer> CPUBurstProcess = new ArrayList<Integer>();
ArrayList<Integer> IOBurstProcess = new ArrayList<Integer>();
//object variables
int processID;
int CPUBurstIndex; //= 1; //assign these in the constructor
int IOBurstIndex; //= 1;
int CPUBurstTimeRemaining;
int IOBurstTimeRemaining;
int PCBPointer; //= 0;
public boolean ReadyOrDisk = true; //T for ReadyQueue, F for DiskQueue
public PCB(ArrayList<Integer> CPUBurstProcess,
ArrayList<Integer> IOBurstProcess, int processID, int CPUBurstIndex,
int IOBurstIndex, int CPUBurstTimeRemaining, int IOBurstTimeRemaining,
int PCBPointer) {
this.CPUBurstProcess = CPUBurstProcess;
this.IOBurstProcess = IOBurstProcess;
this.processID = processID;
this.CPUBurstIndex = 1;
this.IOBurstIndex = 1;
this.CPUBurstTimeRemaining = CPUBurstTimeRemaining;
this.IOBurstTimeRemaining = IOBurstTimeRemaining;
this.PCBPointer = 0;
}
}//end PCB class
I would be happy to show more code or output data if it would be useful. I feel like this is something simple that I am not understanding but I've read through a number of threads that seemed to have similar problems, and none of the suggestions/solutions there were applicable.
Thank you in advance for any help!
Edit:
I tried using the advice in (stackoverflow.com/questions/19843506/…) re: static variables, and changed them to nonstatic variables/ArrayLists (code updated above to match).
However, I am still seeing the same overwriting problem and do not think I am adding the same object twice. There is also no assistance for the suddenly empty ArrayList problem that I am having, in that thread.