//元素
public class ActionPost {
ActionPost next;
public String name;
public ActionPost(String name){
this.name = name;
}
}
public class ActionPostQueue {
private ActionPost head;//永远是最先进栈的元素 头
private ActionPost tail;//永远是最后进栈元素 尾
public synchronized void enqueue(ActionPost pendingPost) {
if (pendingPost == null) {
throw new NullPointerException("null cannot be enqueued");
}
if (tail != null) {
tail.next = pendingPost;//核心代码 tail.next指向的是上一个p的next地址 给其赋值
tail = pendingPost; //尾部替换pendingPost
} else if (head == null) {
head = tail = pendingPost;
} else {
throw new IllegalStateException("Head present, but no tail");
}
notifyAll();
}
public synchronized ActionPost poll() {
ActionPost pendingPost = head;
if (head != null) {
head = head.next;
if (head == null) {
tail = null;
}
}
return pendingPost;
}
synchronized ActionPost poll(int maxMillisToWait) throws InterruptedException {
if (head == null) {
wait(maxMillisToWait);
}
return poll();
}
}