摘要 自定义一个先进先出栈

//元素
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();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值