📄 actorpool.java
字号:
public class ActorPool {
private Actor firstFree;
private Actor lastFree;
private Actor firstUsed;
private Actor lastUsed;
private Actor oldUsed;
public ActorPool(Actor[] initObjects) {
if (initObjects == null || initObjects.length < 2) {
return;
}
firstFree=initObjects[0] ;
lastFree=initObjects[initObjects.length - 1];
for (int i = 0; i < initObjects.length; i++) {
if (i < initObjects.length - 1) {
initObjects[i].setNextLink(initObjects[i + 1]);
} else {
initObjects[i].setNextLink(null);
}
if (i > 0) {
initObjects[i].setPrevLink(initObjects[i - 1]);
} else {
initObjects[i].setPrevLink(null);
}
}
}
public Actor getNextFree() {
if (firstFree != null) {
if (firstFree.getNextLink() != null) {
firstFree.getNextLink().setPrevLink(null);
}
Actor newFirstFree = firstFree.getNextLink();
firstFree.setNextLink(null);
if (lastUsed != null) {
firstFree.setPrevLink(lastUsed);
lastUsed.setNextLink(firstFree);
}
lastUsed = firstFree;
if (firstUsed == null) {
firstUsed = firstFree;
}
if (lastFree == firstFree) {
lastFree = null;
}
Actor actorToReturn = firstFree;
firstFree = newFirstFree;
return actorToReturn;
}
if(oldUsed==null){
oldUsed=firstUsed;
}
Actor actorToReturn = oldUsed;
oldUsed = oldUsed.getNextLink();
return actorToReturn;
}
// public void dump()
// {
// System.out.print("FREE LIST: ");
// Actor a = firstFree;
// while (a != null)
// {
// System.out.print("{" + a + "} ");
// a = a.getNextLink();
// }
// System.out.println("");
//
// System.out.print("USED LIST: ");
// a = firstUsed;
// while (a != null)
// {
// System.out.print("{" + a + "} ");
// a = a.getNextLink();
// }
// System.out.println("");
// System.out.println("LastUsed: {" + lastUsed + "} OldestUsed: {" + oldestUsed +
// "} firstUsed: {" + firstUsed +
// "} LastFree: {" + lastFree + "} firstFree: {" + firstFree + "}");
//
// }
public void release(Actor actorToRelease) {
if (actorToRelease.getPrevLink() != null) {
actorToRelease.getPrevLink().setNextLink(actorToRelease.
getNextLink());
}
if (actorToRelease.getNextLink() != null) {
actorToRelease.getNextLink().setPrevLink(actorToRelease.
getPrevLink());
}
if (firstUsed == actorToRelease) {
firstUsed = actorToRelease.getNextLink();
}
actorToRelease.setNextLink(null);
if (lastUsed == actorToRelease) {
lastUsed = actorToRelease.getPrevLink();
}
if (firstFree == null) {
firstFree = actorToRelease;
actorToRelease.setPrevLink(null);
} else {
actorToRelease.setPrevLink(lastFree);
lastFree.setNextLink(actorToRelease);
}
lastFree = actorToRelease;
}
public Actor getFirstUsed() {
return firstUsed;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -