b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

链表 结构 解读 Java 并发队列 BlockingQueue(3)

电脑杂谈  发布时间:2018-02-21 23:42:01  来源:网络整理

public LinkedBlockingQueue() {

this(Integer.MAX_VALUE);

}

// 传说中的有界队列

public LinkedBlockingQueue(int capacity) {

if (capacity <= 0) throw new IllegalArgumentException();

this.capacity = capacity;

last = head = new Node<E>(null);

}

我们看看这个类有哪些属性:

// 队列容量

private final int capacity;

// 队列中的元素数量

private final AtomicInteger count = new AtomicInteger(0);

// 队头

private transient Node<E> head;

// 队尾

private transient Node<E> last;

// take, poll, peek 等读操作的方法需要获取到这个锁

private final ReentrantLock takeLock = new ReentrantLock();

// 如果读操作的时候队列是空的,那么等待 notEmpty 条件

private final Condition notEmpty = takeLock.newCondition();

// put, offer 等写操作的方法需要获取到这个锁

private final ReentrantLock putLock = new ReentrantLock();

// 如果写操作的时候队列是满的,那么等待 notFull 条件

private final Condition notFull = putLock.newCondition();

这里用了两个锁,两个 Condition,简单介绍如下:

takeLock 和 notEmpty 怎么搭配:如果要获取(take)一个元素,需要获取 takeLock 锁,但是获取了锁还不够,如果队列此时为空,还需要队列不为空(notEmpty)这个条件(Condition)。

putLock 需要和 notFull 搭配:如果要插入(put)一个元素,需要获取 putLock 锁,但是获取了锁还不够,如果队列此时已满,还需要队列不是满的(notFull)这个条件(Condition)。

首先,这里用一个来看看 LinkedBlockingQueue 的并发读写控制,然后再开始分析源码:

看懂这个,源码也就简单了,读操作是排好队的,写操作也是排好队的,唯一的并发问题在于一个写操作和一个读操作同时进行,只要控制好这个就可以了。

先上构造方法:

public LinkedBlockingQueue(int capacity) {

if (capacity <= 0) throw new IllegalArgumentException();

this.capacity = capacity;

last = head = new Node<E>(null);

}

注意,这里会初始化一个空的头结点,那么第一个元素入队的时候,队列中就会有两个元素。读取元素时,也总是获取头节点后面的一个节点。count 的计数值不包括这个头节点。链表 结构

我们来看下 put 方法是怎么将元素插入到队尾的:

public void put(E e) throws InterruptedException {


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-87792-3.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...