jdk 常用的queue
来源:互联网 发布:哪的java技术培训好 编辑:程序博客网 时间:2024/06/07 01:23
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();
SynchronousQueue,没有容量或者"只有一个长度",入队的线程直接阻塞,直到有出队的线程出现,双方交换数据,有点类似握手
有公平非公平两种模式,默认非公平,
阅读全文
0 0
- jdk 常用的queue
- JDK常用的package
- JDK常用的包
- JDK QUEUE队列
- jdk中的queue
- 【备忘】stack和queue的常用参数
- C++的STL库常用API--queue
- 队列<queue>中常用的函数
- JDK 5.0中新的基本接口, Queue 接口
- JDK 中常用的包
- JDK 中常用的包
- JDK 中常用的包
- JDK中常用的包
- 常用Message Queue对比
- 常用Message Queue对比
- 栈stack和队列queue的常用基本函数
- C++ 中 queue(队列)的用法(常用)
- JDK的概念、组成及JDK常用包
- Redis 高可用集群
- uboot-relocation学习总结
- LeetCode刷题(48)--Remove Duplicates from Sorted List II
- 初探OAuth2.0第三方认证登录
- 菜鸡的Django学习笔记(一)Linux+deepin+pycharm
- jdk 常用的queue
- 现代软件工程_团队项目_阿尔法阶段_第六次会议记录_2017.11.30
- 之前项目再集成webService发布服务 + log4j框架日志文件
- 程序员 30 岁前,该如何规划自己的职业发展?
- PyCharm3.0默认快捷键
- 单链表的归并排序
- OpenMV固件升级
- iOS面试题:腾讯一、二面以及参考思路
- Office 2016 for Mac 免费永久使用方式