/*******************************************************************************
 * Copyright (c) 2010-2012 by Min Cai (min.cai.china@gmail.com).
 *
 * This file is part of the Archimulator multicore architectural simulator.
 *
 * Archimulator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Archimulator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Archimulator. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package archimulator.util.event;

import archimulator.util.action.Action;

import java.util.PriorityQueue;

public final class CycleAccurateEventQueue {
    private long currentCycle;
    private final PriorityQueue<CycleAccurateEvent> events;

    public CycleAccurateEventQueue() {
        this.events = new PriorityQueue<CycleAccurateEvent>();
    }

    public synchronized void advanceOneCycle() {
        while (!this.events.isEmpty()) {
            CycleAccurateEvent event = this.events.peek();

            if (event.getWhen() > this.currentCycle) {
                break;
            }

            event.getAction().apply();
            this.events.remove(event);
        }

        this.currentCycle++;
    }

    public CycleAccurateEventQueue schedule(Object sender, Action action, int delay) {
        this.schedule(new CycleAccurateEvent(sender, action, this.currentCycle + delay));
        return this;
    }

    private void schedule(CycleAccurateEvent event) {
        this.events.add(event);
    }

    public void resetCurrentCycle() {
        for (CycleAccurateEvent event : this.events) {
            event.setWhen(event.getWhen() - this.currentCycle);
        }

        this.currentCycle = 0;
    }

    public long getCurrentCycle() {
        return this.currentCycle;
    }

    @Override
    public String toString() {
        return String.format("CycleAccurateEventQueue{currentCycle=%d, events=%s}", currentCycle, events);
    }
}

@@ -1039,29 +1039,17 @@
 va.util.
-PriorityQueue
+*
 ;%0A%0Apubli
@@ -1137,29 +1137,20 @@
 e final 
-PriorityQueue
+List
 %3CCycleAc
@@ -1240,21 +1240,17 @@
 new 
-PriorityQueue
+ArrayList
 %3CCyc
@@ -1291,21 +1291,8 @@
 lic 
-synchronized 
 void
@@ -1349,24 +1349,63 @@
 sEmpty()) %7B%0A
+            Collections.sort(events);%0A%0A
             
@@ -1447,13 +1447,13 @@
 nts.
-peek(
+get(0
 );%0A%0A

Use List and Collections.sort(..) to replace PriorityQueue in CycleAccurateEventQueue.