/*
* Title: CloudSim Toolkit
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2009-2010, The University of Melbourne, Australia
*/
package org.cloudbus.cloudsim.core;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
/**
* 这个类是defer队列,使用linkedlist结构,元素按事件发生时间排序
* 方法:
* 添加事件
* 返回迭代器
* 返回大小
* 清除队列
* This class implements the deferred event queue used by {@link Simulation}.
* The event queue uses a linked list to store the events.
*
* @author Marcos Dias de Assuncao
* @since CloudSim Toolkit 1.0
*
* @see Simulation
* @see SimEvent
*/
public class DeferredQueue {
/** The list. */
private final List<SimEvent> list = new LinkedList<SimEvent>();
/**
* The max time.
* 事件发生的最晚时间
* */
private double maxTime = -1;
/**
* 事件加入队列时,维护元素的顺序,按事件发生事件顺序加入
* Adds a new event to the queue. Adding a new event to the queue
* preserves the temporal order of the events.
* @param newEvent The event to be added to the queue.
*/
public void addEvent(SimEvent newEvent) {
// The event has to be inserted as the last of all events
// with the same event_time(). Yes, this matters.
//返回时间必须发生的时间
double eventTime = newEvent.eventTime();
if (eventTime >= maxTime) {
list.add(newEvent);
maxTime = eventTime;
return;
}
ListIterator<SimEvent> iterator = list.listIterator();
SimEvent event;
//从第一个元素开始搜索
while (iterator.hasNext()) {
event = iterator.next();
if (event.eventTime() > eventTime) {
iterator.previous();
iterator.add(newEvent);
return;
}
}
//队列为空
list.add(newEvent);
}
/**
* Returns an iterator to the events in the queue.
*
* @return the iterator
*/
public Iterator<SimEvent> iterator() {
return list.iterator();
}
/**
* Returns the size of this event queue.
*
* @return the number of events in the queue.
*/
public int size() {
return list.size();
}
/**
* Clears the queue.
*/
public void clear(){
list.clear();
}
}
云计算仿真工具中文注释DeferredQueue.java
最新推荐文章于 2022-01-15 05:15:23 发布