Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why am I stuck in an infinite loop after invoking poll() or remove() on a priority queue? Java

Tags:

java

I get stuck in an infinite for loop if I invoke the remove() or poll() on the priority queue below.

I am trying to print out all the elements in there. So every time I print out the last attribute of the Process elements, I call remove() or poll() on it. And I get stuck in an infinite loop.

public class SPN {

    private int CPU_Burst_Cycles = 0;
    PriorityQueue<Process> prq;
    PriorityQueue<Process> ready_queue;
    Process current;

    public SPN(PriorityQueue<Process> prqPass) {

        //Priority Queue of the processes
        prq = prqPass;

        System.out.println(prq.size());
        for(int i = 0; i<prq.size(); i++) {

            //THIS IS WHERE I GET IN TROUBLE
            System.out.println(prq.peek().getName() + "'s rem time: " + prq.remove().getRemTime());

            System.out.println("Actual size: " + prq.size());

        }
.
.
.
.
}

Process class:

public class Process {

    private String name;
    private int arrive_time= 0;
    private int burst_time = 0;
    private int remain_time = 0;

    public Process (String name, int arr_time, int bur_time) {

        this.arrive_time = arr_time;
        this.burst_time = bur_time;
        this.remain_time = burst_time;
        this.name = name;
    }

    public int getArrTime() {return arrive_time;}
    public int getBurTime() {return burst_time;}
    public int getRemTime() {return remain_time;}
    public String getName() {return name;}

    public void decRemTime() {this.remain_time--;}
}

Then in a third class I create a SPN instance and I pass it a priority queue. If I just do a peek() it works fine.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

    public class Test {

        //Priority READY_QUEUE for the accessed processes
        public static PriorityQueue<Process> prq = new PriorityQueue<Process>(5, new Comparator<Process> () {

            @Override
            public int compare(Process p1, Process p2) {
                return p1.getArrTime() - p2.getArrTime();
            }
        });

        public static void main(String[] args) throws IOException {

            BufferedReader br = null;
            String line;

            try {
                br =  new BufferedReader(new FileReader("C:\\Users\\Veni\\Desktop\\test\\test.txt\\"));
            }
            catch (FileNotFoundException fnfex) {
                System.out.println(fnfex.getMessage() + "File not found");
                System.exit(0);
            }

            while((line = br.readLine()) != null) {

                String[] params = line.split(" ");
                prq.add(new Process(params[0], Integer.parseInt(params[1]), Integer.parseInt(params[2]) ));
            }       
            SPN spn = new SPN(prq);
            spn.SPN_ALG();
        }
    }
like image 560
Rumen Hristov Avatar asked Feb 03 '26 19:02

Rumen Hristov


1 Answers

The Iterator.remove() JavaDoc says (in part)

This method can be called only once per call to next(). The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.

public SPN(PriorityQueue<Process> prq) {
    System.out.println(prq.size());
    Iterator<Process> iter = prq.iterator();
    while (iter.hasNext()) {
        Process p = iter.next();
        System.out.println(p.getName() + "'s rem time: "
                + p.getRemTime());
        iter.remove();
        System.out.println("Actual size: " + prq.size());
    }
}
like image 108
Elliott Frisch Avatar answered Feb 05 '26 08:02

Elliott Frisch