gpt4 book ai didi

java - 为什么我在优先级队列上调用 poll() 或 remove() 后陷入无限循环? java

转载 作者:行者123 更新时间:2023-11-30 11:05:22 28 4
gpt4 key购买 nike

如果我在下面的优先级队列上调用 remove()poll(),我就会陷入无限循环。

我正在尝试打印出其中的所有元素。因此,每次我打印出 Process 元素的最后一个属性时,我都会对其调用 remove()poll()。我陷入了无限循环。

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());

}
.
.
.
.
}

进程类:

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--;}
}

然后在第三个类中,我创建了一个 SPN 实例,并向它传递了一个优先级队列。如果我只是做一个 peek() 它就可以正常工作。

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();
}
}

最佳答案

Iterator.remove() JavaDoc说(部分)

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());
}
}

关于java - 为什么我在优先级队列上调用 poll() 或 remove() 后陷入无限循环? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29664409/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com