赞
踩
可能报异常 | 返回布尔值 | 可能阻塞 | 设定等待时间 | |
入队 | add(e) | offer(e) | put(e) | offer(e, timeout, unit) |
出队 | remove() | poll() | take() | poll(timeout, unit) |
查看 | element() | peek() | 无 | 无 |
- public boolean add(E e) {
- if (offer(e))
- return true;
- else
- throw new IllegalStateException("Queue full");//队列已满,抛异常
- }
-
- public E remove() {
- E x = poll();
- if (x != null)
- return x;
- else
- throw new NoSuchElementException();//队列为空,抛异常
- }
- public boolean offer(E e) {
- if (e == null) throw new NullPointerException();
- final ReentrantLock lock = this.lock;
- lock.lock();
- try {
- if (count == items.length)//队列已满,返回false
- return false;
- else {
- insert(e);//insert方法中发出了notEmpty.signal();
- return true;
- }
- } finally {
- lock.unlock();
- }
- }
-
- public E poll() {
- final ReentrantLock lock = this.lock;
- lock.lock();
- try {
- if (count == 0)//队列为空,返回false
- return null;
- E x = extract();//extract方法中发出了notFull.signal();
- return x;
- } finally {
- lock.unlock();
- }
- }
- public void put(E e) throws InterruptedException {
- if (e == null) throw new NullPointerException();
- final E[] items = this.items;
- final ReentrantLock lock = this.lock;
- lock.lockInterruptibly();
- try {
- try {
- while (count == items.length)//如果队列已满,等待notFull这个条件,这时当前线程被阻塞
- notFull.await();
- } catch (InterruptedException ie) {
- notFull.signal(); //唤醒受notFull阻塞的当前线程
- throw ie;
- }
- insert(e);
- } finally {
- lock.unlock();
- }
- }
-
- public E take() throws InterruptedException {
- final ReentrantLock lock = this.lock;
- lock.lockInterruptibly();
- try {
- try {
- while (count == 0)//如果队列为空,等待notEmpty这个条件,这时当前线程被阻塞
- notEmpty.await();
- } catch (InterruptedException ie) {
- notEmpty.signal();//唤醒受notEmpty阻塞的当前线程
- throw ie;
- }
- E x = extract();
- return x;
- } finally {
- lock.unlock();
- }
- }
- static class Node<E> {
- /** The item, volatile to ensure barrier separating write and read */
- volatile E item;
- Node<E> next;
- Node(E x) { item = x; }
- }
- /** 头指针 */
- private transient Node<E> head; //head.next是队列的头元素
- /** 尾指针 */
- private transient Node<E> last; //last.next是null
- private void enqueue(E x) {
- last = last.next = new Node<E>(x); //入队是为last再找个下家
- }
-
- private E dequeue() {
- Node<E> first = head.next; //出队是把head.next取出来,然后将head向后移一位
- head = first;
- E x = first.item;
- first.item = null;
- return x;
- }
- private final AtomicInteger count = new AtomicInteger(0);
- /** 用于读取的独占锁*/
- private final ReentrantLock takeLock = new ReentrantLock();
- /** 队列是否为空的条件 */
- private final Condition notEmpty = takeLock.newCondition();
- /** 用于写入的独占锁 */
- private final ReentrantLock putLock = new ReentrantLock();
- /** 队列是否已满的条件 */
- private final Condition notFull = putLock.newCondition();
- public boolean offer(E e) {
- if (e == null) throw new NullPointerException();
- final AtomicInteger count = this.count;
- if (count.get() == capacity)
- return false;
- int c = -1;
- final ReentrantLock putLock = this.putLock;//入队当然用putLock
- putLock.lock();
- try {
- if (count.get() < capacity) {
- enqueue(e); //入队
- c = count.getAndIncrement(); //队长度+1
- if (c + 1 < capacity)
- notFull.signal(); //队列没满,当然可以解锁了
- }
- } finally {
- putLock.unlock();
- }
- if (c == 0)
- signalNotEmpty();//这个方法里发出了notEmpty.signal();
- return c >= 0;
- }
-
- public E poll() {
- final AtomicInteger count = this.count;
- if (count.get() == 0)
- return null;
- E x = null;
- int c = -1;
- final ReentrantLock takeLock = this.takeLock;出队当然用takeLock
- takeLock.lock();
- try {
- if (count.get() > 0) {
- x = dequeue();//出队
- c = count.getAndDecrement();//队长度-1
- if (c > 1)
- notEmpty.signal();//队列没空,解锁
- }
- } finally {
- takeLock.unlock();
- }
- if (c == capacity)
- signalNotFull();//这个方法里发出了notFull.signal();
- return x;
- }
- public boolean offer(E e) {
- if (e == null) throw new NullPointerException();
- Node<E> n = new Node<E>(e, null);
- for (;;) {
- Node<E> t = tail;
- Node<E> s = t.getNext();
- if (t == tail) { //------------------------------a
- if (s == null) { //---------------------------b
- if (t.casNext(s, n)) { //-------------------c
- casTail(t, n); //------------------------d
- return true;
- }
- } else {
- casTail(t, s); //----------------------------e
- }
- }
- }
- }
- if(!queue.isEmpty()) {
- queue.poll(obj);
- }
- synchronized(queue) {
- if(!queue.isEmpty()) {
- queue.poll(obj);
- }
- }
- public final class Counter {
- private long value = 0;
- public synchronized long getValue() {
- return value;
- }
- public synchronized long increment() {
- return ++value;
- }
- }
- public class NonblockingCounter {
- private AtomicInteger value;//前面提到过,AtomicInteger类是以原子的方式操作整型变量。
- public int getValue() {
- return value.get();
- }
- public int increment() {
- int v;
- do {
- v = value.get();
- while (!value.compareAndSet(v, v + 1));
- return v + 1;
- }
- }
- public class LinkedQueue <E> {
- private static class Node <E> {
- final E item;
- final AtomicReference<Node<E>> next;
- Node(E item, Node<E> next) {
- this.item = item;
- this.next = new AtomicReference<Node<E>>(next);
- }
- }
- private AtomicReference<Node<E>> head
- = new AtomicReference<Node<E>>(new Node<E>(null, null));
- private AtomicReference<Node<E>> tail = head;
- public boolean put(E item) {
- Node<E> newNode = new Node<E>(item, null);
- while (true) {
- Node<E> curTail = tail.get();
- Node<E> residue = curTail.next.get();
- if (curTail == tail.get()) {
- if (residue == null) /* A */ {
- if (curTail.next.compareAndSet(null, newNode)) /* C */ {
- tail.compareAndSet(curTail, newNode) /* D */ ;
- return true;
- }
- } else {
- tail.compareAndSet(curTail, residue) /* B */;
- }
- }
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。