我有两个计时器来管理来自 FIFO 队列的输入(入队)和输出(出队),但我不断收到出队 java.lang.IllegalStateException: 计时器已取消的异常。我无法停止调试声称错误发生在第 83 行的行。我不知道我缺少什么,因此将不胜感激。
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
/**
* RunSim
*/
public class RunSim {
private double arrivalRate = 600;
private double y;
private Timer t;
private Timer t2;
private Queue fifoQueue;
private long xy;
private long fact = 10;
private int count;
private int pId;
public RunSim() {
Random r = new Random();
long n = System.currentTimeMillis();
r.setSeed(n);
double i = r.nextDouble();
y = ((1 / arrivalRate) * (Math.log(i)));
xy = (long) y;
t = new Timer();
t2 = new Timer();
fifoQueue = new Queue();
count = 0;
pId = 0;
}
public static void main() {
RunSim rs = new RunSim();
rs.start();
}
public void start() {
class sendPacket extends TimerTask {
public void run() {
Packet p = new Packet();
p.setId(pId);
fifoQueue.insert(p);
p.setArrivalTime();
System.out.println("ID: " + p.getId() + " Arrival Time: "
+ p.getArrivalTime() / fact);
pId++;
}
}
class removePacket extends TimerTask {
public void run() {
fifoQueue.first().setDepartureTime();
System.out.println("ID: " + fifoQueue.first().getId()
+ " Departure Time: "
+ fifoQueue.first().getDepartureTime() / fact);
fifoQueue.remove();
}
}
while (count < 1000) {
long v = fact * (1 + Math.abs(xy));
t.schedule(new sendPacket(), 0, v);
count++;
t2.schedule(new removePacket(), 5, 5);
}
}
}