我对你的问题很感兴趣,所以我想出了这个解决方案,它可能对你有用。该代码中没有健全性检查,它只是一个概念证明。
事件:
public class CloudSimEvent {
private final int id;
private final boolean allocated;
public CloudSimEvent(int id, boolean allocated) {
super();
this.id = id;
this.allocated = allocated;
}
public int getID() {
return id;
}
public boolean allocated() {
return allocated;
}
public boolean finished() {
return !allocated;
}
}
事件界面
public interface CloudSimEventListener {
public void cloudSimEventFired(CloudSimEvent e);
}
VMAllocation 对象
public final class VMAllocation implements Comparable<VMAllocation> {
private final int id;
private final int maxAlloc;
private final int currAlloc;
public VMAllocation(int id, int maxAlloc, int currAlloc) {
super();
this.id = id;
this.maxAlloc = maxAlloc;
this.currAlloc = currAlloc;
}
public VMAllocation allocate() {
return new VMAllocation(id, maxAlloc, currAlloc + 1);
}
public VMAllocation finish() {
return new VMAllocation(id, maxAlloc, currAlloc - 1);
}
public boolean isBusy() {
return currAlloc >= maxAlloc;
}
public int getId() {
return id;
}
@Override
public int compareTo(VMAllocation o) {
if (isBusy() && !o.isBusy()) {
return 1;
} else if (!isBusy() && o.isBusy()) {
return -1;
} else {
return Integer.compare(id, o.id);
}
}
@Override
public String toString() {
return "id: " + id + " currAlloc: " + currAlloc + " maxAlloc: "
+ maxAlloc + " is-busy: " + isBusy();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + currAlloc;
result = prime * result + id;
result = prime * result + maxAlloc;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
VMAllocation other = (VMAllocation) obj;
if (currAlloc != other.currAlloc)
return false;
if (id != other.id)
return false;
if (maxAlloc != other.maxAlloc)
return false;
return true;
}
}
RRS调度器
import java.util.Arrays;
import java.util.Iterator;
import java.util.PriorityQueue;
public class RRSheduler implements CloudSimEventListener {
private final PriorityQueue<VMAllocation> queue = new PriorityQueue<>();
public void addVMAllocation(VMAllocation allocation) {
synchronized (queue) {
queue.add(allocation);
}
}
@Override
public void cloudSimEventFired(CloudSimEvent e) {
VMChanged(e.getID(), e.allocated());
}
private void VMChanged(int id, boolean allocation) {
synchronized (queue) {
Iterator<VMAllocation> it = queue.iterator();
VMAllocation newAllocation = null;
while (it.hasNext()) {
VMAllocation vmAllocation = it.next();
if (vmAllocation.getId() == id) {
if (allocation)
newAllocation = vmAllocation.allocate();
else
newAllocation = vmAllocation.finish();
it.remove();
break;
}
}
if (newAllocation != null)
queue.add(newAllocation);
}
}
public VMAllocation getNextFreeVMAllocation() {
synchronized (queue) {
VMAllocation allocation = queue.element();
return allocation.isBusy() ? null : allocation;
}
}
@Override
public String toString() {
synchronized (queue) {
VMAllocation[] arr = queue.toArray(new VMAllocation[] {});
Arrays.sort(arr);
return Arrays.toString(arr);
}
}
}
简单的测试程序
public class TestRR {
public static void main(String[] args) {
RRSheduler rrSheduler = new RRSheduler();
rrSheduler.addVMAllocation(new VMAllocation(0, 4, 0));
rrSheduler.addVMAllocation(new VMAllocation(1, 4, 0));
rrSheduler.addVMAllocation(new VMAllocation(2, 2, 0));
rrSheduler.addVMAllocation(new VMAllocation(3, 2, 0));
rrSheduler.addVMAllocation(new VMAllocation(4, 1, 0));
VMAllocation nextAllocation = null;
while ((nextAllocation = rrSheduler.getNextFreeVMAllocation()) != null) {
System.out.println(nextAllocation);
rrSheduler.cloudSimEventFired(new CloudSimEvent(nextAllocation
.getId(), true));
}
}
}
我没有像你那样使用地图,但你可能会知道如何解决你的问题。