## 实现原理

``// 队列容量，最大为Integer.MAX_VALUEprivate final int capacity;// 队列长度private final AtomicInteger count = new AtomicInteger();// 头结点transient Node<E> head;// 尾结点private transient Node<E> last;// 移除操作的锁，take/poll方法用到private final ReentrantLock takeLock = new ReentrantLock();// 移除操作需要等待的条件notEmpty，与takeLock绑定private final Condition notEmpty = takeLock.newCondition();// 入队操作的锁，put/offer方法用到private final ReentrantLock putLock = new ReentrantLock();// 入队操作需要等待的条件notFull，与putLock绑定``private final Condition notFull = putLock.newCondition();``

1.无参构造函数

``public LinkedBlockingQueue() {// 调用有参构造函数，初始化容量capacity为int最大值this(Integer.MAX_VALUE);``}``

2.有参构造函数

``public LinkedBlockingQueue(int capacity) {// 容量不能小于0，注意也不能等于0，这点与常规的集合不同if (capacity <= 0) throw new IllegalArgumentException();this.capacity = capacity;// 初始化头结点和尾结点为哑节点last = head = new Node<E>(null);``}``

3.put()操作

``public void put(E e) throws InterruptedException {if (e == null) throw new NullPointerException();int c = -1;Node<E> node = new Node<E>(e);final ReentrantLock putLock = this.putLock;final AtomicInteger count = this.count;putLock.lockInterruptibly();try {while (count.get() == capacity) {notFull.await();}enqueue(node);c = count.getAndIncrement();if (c + 1 < capacity)notFull.signal();} finally {putLock.unlock();}if (c == 0)signalNotEmpty();``}``

4.put()操作

``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;Node<E> node = new Node<E>(e);final ReentrantLock putLock = this.putLock;putLock.lock();try {if (count.get() < capacity) {enqueue(node);c = count.getAndIncrement();if (c + 1 < capacity)notFull.signal();}} finally {putLock.unlock();}if (c == 0)signalNotEmpty();return c >= 0;``}``

5.take()操作

``public E take() throws InterruptedException {E x;int c = -1;final AtomicInteger count = this.count;final ReentrantLock takeLock = this.takeLock;takeLock.lockInterruptibly();try {while (count.get() == 0) {notEmpty.await();}x = dequeue();c = count.getAndDecrement();if (c > 1)notEmpty.signal();} finally {takeLock.unlock();}if (c == capacity)signalNotFull();return x;``}``

6.poll()操作

``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.lock();try {if (count.get() > 0) {x = dequeue();c = count.getAndDecrement();if (c > 1)notEmpty.signal();}} finally {takeLock.unlock();}if (c == capacity)signalNotFull();return x;``}``

9 声望

1 粉丝

0 条评论

## 实现原理

``// 队列容量，最大为Integer.MAX_VALUEprivate final int capacity;// 队列长度private final AtomicInteger count = new AtomicInteger();// 头结点transient Node<E> head;// 尾结点private transient Node<E> last;// 移除操作的锁，take/poll方法用到private final ReentrantLock takeLock = new ReentrantLock();// 移除操作需要等待的条件notEmpty，与takeLock绑定private final Condition notEmpty = takeLock.newCondition();// 入队操作的锁，put/offer方法用到private final ReentrantLock putLock = new ReentrantLock();// 入队操作需要等待的条件notFull，与putLock绑定``private final Condition notFull = putLock.newCondition();``

1.无参构造函数

``public LinkedBlockingQueue() {// 调用有参构造函数，初始化容量capacity为int最大值this(Integer.MAX_VALUE);``}``

2.有参构造函数

``public LinkedBlockingQueue(int capacity) {// 容量不能小于0，注意也不能等于0，这点与常规的集合不同if (capacity <= 0) throw new IllegalArgumentException();this.capacity = capacity;// 初始化头结点和尾结点为哑节点last = head = new Node<E>(null);``}``

3.put()操作

``public void put(E e) throws InterruptedException {if (e == null) throw new NullPointerException();int c = -1;Node<E> node = new Node<E>(e);final ReentrantLock putLock = this.putLock;final AtomicInteger count = this.count;putLock.lockInterruptibly();try {while (count.get() == capacity) {notFull.await();}enqueue(node);c = count.getAndIncrement();if (c + 1 < capacity)notFull.signal();} finally {putLock.unlock();}if (c == 0)signalNotEmpty();``}``

4.put()操作

``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;Node<E> node = new Node<E>(e);final ReentrantLock putLock = this.putLock;putLock.lock();try {if (count.get() < capacity) {enqueue(node);c = count.getAndIncrement();if (c + 1 < capacity)notFull.signal();}} finally {putLock.unlock();}if (c == 0)signalNotEmpty();return c >= 0;``}``

5.take()操作

``public E take() throws InterruptedException {E x;int c = -1;final AtomicInteger count = this.count;final ReentrantLock takeLock = this.takeLock;takeLock.lockInterruptibly();try {while (count.get() == 0) {notEmpty.await();}x = dequeue();c = count.getAndDecrement();if (c > 1)notEmpty.signal();} finally {takeLock.unlock();}if (c == capacity)signalNotFull();return x;``}``

6.poll()操作

``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.lock();try {if (count.get() > 0) {x = dequeue();c = count.getAndDecrement();if (c > 1)notEmpty.signal();}} finally {takeLock.unlock();}if (c == capacity)signalNotFull();return x;``}``