1

I am fairly new to java and working on a project to simulate a CPU scheduler in Java and i am using a linked list to store each process object that is read in from a external master list. When I test print the processes and the variables they contain, everything comes out as expected but whenever I try and do something with them it stops working.

public class process

String ID;
int Arrive;
int ExecSize;
int Execstore;
int Tstart;
int Tend;
int quant;
public process(String ID,int Arrive,int ExecSize) {
    this.ID = ID;
    this.Arrive = Arrive;
    this.ExecSize = ExecSize;
    this.Execstore=ExecSize;
    this.Tend = 0;
    this.Tstart = 0;
    this.quant = 4;

}

public void setquant(int update) {
    this.quant = update;
}

public int getquant() {
    return quant;
}

public void setExecSize(int update) {
    this.ExecSize = update;
}

public void setTend(int update) {
    this.Tend = update;
}

public void setTstart(int update) {
    this.Tstart = update;
}


String getID() {
    return ID;
}
int getArrive() {
    return Arrive;
}
int getExecSize() {
    return ExecSize;
}
int getTstart() {
    return Tstart;
}
int getTend() {
    return Tend;
}
int getExecstore() {
    return Execstore;
}

and this is the class used for the simulation

public class fcfs {
int disp;
int Ttotal = 0;
int Exec;
int Turn;
int Wait;
String output;
LinkedList<process> Que = new LinkedList<process>();
LinkedList<process> Quecleared = new LinkedList<process>();

public fcfs(LinkedList<process> B,int D) {
    Que.addAll(B);
    disp=D;
}

public void run() 
{

    while (Que != null) 
    {
            Ttotal = Ttotal + disp;
            System.out.println(Que.getFirst().getExecSize());
            Exec=Que.getFirst().getExecSize();
            output += String.format("T%d: %s\n",Ttotal,Que.getFirst().getID());
            Que.getFirst().setTstart(Ttotal);
            Ttotal = Ttotal+Exec;
            Que.getFirst().setTend(Ttotal);
            Quecleared.add(Que.poll());
    }   
}

So whenever i use System.out.println I get the expected result that I read into the list. But anything else I try to do in reference to elements of the process object will not work. Any help would be greatly appreciated

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Nat D
  • 21
  • 3

1 Answers1

0
while (!Que.isEmpty()) 
{
        Ttotal = Ttotal + disp;
        System.out.println(Que.peekFirst().getExecSize());
        Exec=Que.peekFirst().getExecSize();
        output += String.format("T%d: %s\n",Ttotal,Que.peekFirst().getID());
        Que.peekFirst().setTstart(Ttotal);
        Ttotal = Ttotal+Exec;
        Que.peekFirst().setTend(Ttotal);
        Quecleared.add(Que.pollFirst());
}

This shouldn't throw an error on Exec = Que.peekFirst().getExecSize();

That error is thrown when your container is empty.

EDIT

In your code you specified the condition Que != null. In java once an object has been instantiated it is no longer considered null even if it IS empty. Most likely what was happening here is you continued iterating through your while(Que != null) loop until you had called Que.poll() on all elements of the list.

After clearing the list you did not exit the loop because Que still was not null. Then calling getFirst() on the empty instance of a LinkedList threw an exception.

A similar situation can be seen here with null vs empty strings: Difference between null and empty ("") Java String

EDIT 2

It also appears that your class methods for getID(), getExecSize(), etc are passing values by reference as opposed to copying their value. Thus any change you make after passing the reference from queue will alter any copies you tried to make of it.

This can be best avoided by creating a new instance of an object and returning that from your function. Shown in an answer on the question linked below:

class Foo {
    private Bar myBar;
    public Foo deepCopy() {
        Foo newFoo = new Foo();
        newFoo.myBar = myBar.clone(); //or new Bar(myBar) or myBar.deepCopy or ...
        return newFoo;
    }
}

For more information on ways to pass values as opposed to reference values of your pre-existing instances, as well as what a shallow copy actually is, check this link: In Java, what is a shallow copy?

Thunderwood
  • 525
  • 2
  • 9
  • i believe it is running through the loop as it should only print one line each iteration but only the first direct print is generating output nothing else is giving me an out put for the loop i should be getting at least some output from the other functions but i am not getting any – Nat D Sep 11 '18 at 15:49
  • can you add where you implement this code? show me what you're passing in? and a sample of the output you expect for the other lines @NatD – Thunderwood Sep 11 '18 at 16:02
  • @NatD I would like you to try something for me. In your `getID()` method, I want you to remove what you have and replace it with this `String newID = new String(this.ID); return newID;` and tell me if that changes anything – Thunderwood Sep 11 '18 at 16:32