java队列阻塞方法_Java源码解析阻塞队列ArrayBlockingQueue常用方法

Java源码解析阻塞队列ArrayBlockingQueue常用方法

发布于 2020-7-15|

复制链接

分享一篇关于关于Java源码解析阻塞队列ArrayBlockingQueue常用方法,小妖觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小妖来看看吧

本文基于jdk1.8进行分析

```java

/** 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条件上等待的线程。

```java

/**

* 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方法那样,当队列满时会一直等待。

```java

/**

* 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,则等待不空条件,否则进行出队操作。

```java

public E take() throws InterruptedException {

final ReentrantLock lock = this.lock;

lock.lockInterruptibly();

try {

while (count == 0)

notEmpty.await();

return dequeue();

} finally {

lock.unlock();

}

}

```

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值