0

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:

  1. Each time an object is added to the ArrayList, it overwrites all previous objects with the new values (along with adding the new object).
  2. 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.

  • I tried using the advice in (https://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list) re: static variables, and changed them to nonstatic variables/ArrayLists. However, I am still seeing the same problem. There is also no assistance for the suddenly empty ArrayList in that thread. – magicandflew May 09 '16 at 03:27
  • You called `clear` on your lists, so that's most likely why they are empty – OneCricketeer May 09 '16 at 04:06
  • Or you just aren't adding to any list other than `jobQueue`, so it's not clear why there should be data in the other lists – OneCricketeer May 09 '16 at 04:09
  • @cricket_007 Well, removing the `clear` did fix the problem of the empy ArrayList, though it still overwrites all the previous objects instead of each having an ArrayList of values. Thank you for that, it never occurred to me that it might be the problem and maybe I can use it to help isolate what's going on. The data being transferred out of `jobQueue` is in a separate method that didn't seem relevant for this question. – magicandflew May 09 '16 at 04:17
  • 1
    Note that each list is only a *reference* object. If you update one list object from one method, outside the PCB class, for example, it is also updated in the PCB class. – OneCricketeer May 09 '16 at 04:53
  • @cricket_007 Okay, that makes more sense now! Here I'd thought each time it created an object and passed in a list, that each object would store a separate copy of that list. – magicandflew May 09 '16 at 06:08
  • Only when you do `new ArrayList` do you have a new reference – OneCricketeer May 09 '16 at 12:29

0 Answers0