jdk 常用的queue

queue队列,先进先出


1、优先级队列,元素有优先级

public class PriorityQueue<E> extends AbstractQueue<E> implements java.io.Serializable

队列使用堆排序,二叉树,使用数组保存数据,非线程安全

入队,新节点一直跟自己的父节点比较,不复合比较条件,交换位置,直到根节点

 public boolean offer(E e) {
        if (e == null)
            throw new NullPointerException();
        modCount++;
        int i = size;
        if (i >= queue.length)
            grow(i + 1);
        size = i + 1;
        if (i == 0)
            queue[0] = e;
        else
            siftUp(i, e);
        return true;
    }

 private void siftUp(int k, E x) {
        if (comparator != null)
            siftUpUsingComparator(k, x);
        else
            siftUpComparable(k, x);
    }

private void siftUpUsingComparator(int k, E x) {
        while (k > 0) {
            int parent = (k - 1) >>> 1;
            Object e = queue[parent];
            if (comparator.compare(x, (E) e) >= 0)
                break;
            queue[k] = e;
            k = parent;
        }
        queue[k] = x;
    }


出队,取出根节点值,把最后一个节点放到根节点,然后比较根节点和左右两个子节点,找到正确位置

 private void siftDownUsingComparator(int k, E x) {
        int half = size >>> 1;
        while (k < half) {
            int child = (k << 1) + 1;
            Object c = queue[child];
            int right = child + 1;
            if (right < size &&
                comparator.compare((E) c, (E) queue[right]) > 0)
                c = queue[child = right];
            if (comparator.compare(x, (E) c) <= 0)
                break;
            queue[k] = c;
            k = child;
        }
        queue[k] = x;
    }

对应线程安全的PriorityBlockingQueue,使用ReentrantLock,在入队出队总量等方法加锁,出队无值时候加入Condition队列


2、延时队列,元素有优先级和延时时间,出队时,时间未到,线程等待

public class DelayQueue<E extends Delayed> extends AbstractQueue<E> implements BlockingQueue<E> 
返回并移出队列头,如果元素没有到过期时间,则等待

 public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            for (;;) {
                E first = q.peek();
                if (first == null)
                    available.await();
                else {
                    long delay = first.getDelay(NANOSECONDS);
                    if (delay <= 0)
                        return q.poll();
                    first = null; // don't retain ref while waiting
                    if (leader != null)// 已经有一个线程先来取值了,则当前线程无期等待
                        available.await();
                    else {
                        Thread thisThread = Thread.currentThread();//设置当前线程为最先来取值的线程,
                        leader = thisThread;
                        try {
                            available.awaitNanos(delay);//等待指定时间,到时间自动唤醒
                        } finally {
                            if (leader == thisThread)
                                leader = null;
                        }
                    }
                }
            }
        } finally {
            if (leader == null && q.peek() != null)
                available.signal();//leader线程需要取完值后唤醒等待队列上的第一个线程,使它去尝试取新的队列头
            lock.unlock();
        }
    }


3、阻塞队列BlockingQueue,即入队时空间不足,线程等待有空间时继续入队,出队时队列为空,线程等待直到有元素

常用的有ArrayBlockingQueue,LinkedBlockingQueue,SynchronousQueue


ArrayBlockingQueue,初始化有界数组,数组存储

public ArrayBlockingQueue(int capacity, boolean fair) {
        if (capacity <= 0)
            throw new IllegalArgumentException();
        this.items = new Object[capacity];
        lock = new ReentrantLock(fair);
        notEmpty = lock.newCondition();
        notFull =  lock.newCondition();
    }

两个Condition,分别用来等待入队的线程,和出队的线程,入队后唤醒notEmpty,出队后唤醒notFull


LinkedBlockingQueue,有界,或者默认Integer.MAX_VALUE,使用链表存储节点,

 /** Lock held by take, poll, etc */
    private final ReentrantLock takeLock = new ReentrantLock();

    /** Wait queue for waiting takes */
    private final Condition notEmpty = takeLock.newCondition();

    /** Lock held by put, offer, etc */
    private final ReentrantLock putLock = new ReentrantLock();

    /** Wait queue for waiting puts */
    private final Condition notFull = putLock.newCondition();


使用了两个ReentrantLock分别控制入队出队,原理基本和ArrayBlockingQueue一致,Executors.newFixedThreadPool使用此队列


SynchronousQueue,没有容量或者"只有一个长度",入队的线程直接阻塞,直到有出队的线程出现,双方交换数据,有点类似握手

有公平非公平两种模式,默认非公平,



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值