本文基于jdk1.8进行分析
arrayblockingqueue的功能简介参考http://www.zzvips.com/article/174337.html。
首先看一下arrayblockingqueue的成员变量。如下图。最主要的成员变量是items,它是一个object类型的数组用于保存阻塞队列中的元素。其次是takeindex,putindex,count,分别表示了从队列获取元素的位置,往队列里放元素的位置和队列中元素的个数。然后是lock,notempty和notfull三个和锁相关的成员变量。lock是一个可重入锁,而notempty和notfull是和lock绑定的2个condition。对可重入锁不是很了解的同学,可以参考这篇文章http://www.zzvips.com/article/174043.html。对可重入锁的理解,是理解arrayblockingqueue的基础。也可以这么说,理解了可重入锁,那么在理解arrayblockingqueue就很顺利了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/** the queued items **/
final object[] items;
/** items index for next take, poll, peek or remove **/
int takeindex;
/** items index for next put, offer, or add **/
int putindex;
/** number of elements in the queue **/
int count;
/**
* concurrency control uses the classic two-condition algorithm
* found in any textbook.
**/
/** main lock guarding all access **/
final reentrantlock lock;
/** condition for waiting takes **/
private final condition notempty;
/** condition for waiting puts **/
private final condition notfull;
/**
* shared state for currently active iterators, or null if there
* are known not to be any. allows queue operations to update
* iterator state.
**/
transient itrs itrs = null ;
|
接下来介绍arrayblockingqueue的主要方法。首先是入队方法。arrayblockingqueue的入队方法有好几个,功能略有差异,下面我们逐一介绍各个入队方法。首先看一下put方法,如下图。put方法的功能是,往队列尾部插入指定元素,如果队列已满,那么就等待可用空间。方法的实现过程是,首先判断元素是否非空。然后,进行加锁,加锁后判断队列是否已满。如果已满,则等待不满条件。当被唤醒后,进行入队操作。入队方法中,会唤醒在notempty条件上等待的线程。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/**
* inserts the specified element at the tail of this queue, waiting
* for space to become available if the queue is full.
* @throws interruptedexception {@inheritdoc}
* @throws nullpointerexception {@inheritdoc}
**/
public void put(e e) throws interruptedexception {
checknotnull(e);
final reentrantlock lock = this .lock;
lock.lockinterruptibly();
try {
while (count == items.length)
notfull.await();
enqueue(e);
} finally {
lock.unlock();
}
}
/**
* inserts element at current put position, advances, and signals.
* call only when holding lock.
**/
private void enqueue(e x) {
// assert lock.getholdcount() == 1;
// assert items[putindex] == null;
final object[] items = this .items;
items[putindex] = x;
if (++putindex == items.length)
putindex = 0 ;
count++;
notempty.signal();
}
|
另一个入队方法是offer,代码如下。这个方法与add方法的区别是,offer方法是立刻返回的,它并不像add方法那样,当队列满时会一直等待。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/**
* inserts the specified element at the tail of this queue if it is
* possible to do so immediately without exceeding the queue's capacity,
* returning {@code true} upon success and {@code false} if this queue
* is full. this method is generally preferable to method {@link #add},
* which can fail to insert an element only by throwing an exception.
* @throws nullpointerexception if the specified element is null
**/
public boolean offer(e e) {
checknotnull(e);
final reentrantlock lock = this .lock;
lock.lock();
try {
if (count == items.length)
return false ;
else {
enqueue(e);
return true ;
}
} finally {
lock.unlock();
}
}
|
接下来看一下出队方法take,代码如下。首先对可重入锁加锁,然后判断元素个数是否为0.如果为0,则等待不空条件,否则进行出队操作。
1
2
3
4
5
6
7
8
9
10
11
|
public e take() throws interruptedexception {
final reentrantlock lock = this .lock;
lock.lockinterruptibly();
try {
while (count == 0 )
notempty.await();
return dequeue();
} finally {
lock.unlock();
}
}
|
arraylistblockingqueue中还有其他相关方法,这里就不一一介绍了。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/li_canhui/article/details/85063057