LinkedBlockingQueue与ArrayBlockingQueue的区别

LinkedBlockingQueue

看LinkedBlockingQueue之前可以参考前一篇:ArrayBlockingQueue源码解读

例子

将前面的例子改为LinkedBlockingQueue实现,我们看到,程序依然可以正常运行。

package juc;

import org.junit.Test;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class LinkedBlockingQueueTest {

	@Test
	public void test() throws InterruptedException {
		final BlockingQueue blockingQueue = new LinkedBlockingQueue(10);
		// 新建两个生产者线程+一个消费者线程,(生产是比消费快的,当缓冲区满时,生产者会阻塞)
		Thread t1 = new Thread(new Producer(blockingQueue), "生产者1");
		Thread t2 = new Thread(new Producer(blockingQueue), "生产者2");
		Thread t3 = new Thread(new Consumer(blockingQueue), "消费者1");
		t1.start();
		t2.start();
		t3.start();
		t1.join();
		t2.join();
		t3.join();
	}

	class Consumer implements Runnable {

		private BlockingQueue blockingQueue;

		Consumer(BlockingQueue blockingQueue) {
			this.blockingQueue = blockingQueue;
		}

		@Override
		public void run() {
			for (; ; ) {
				System.out.println(Thread.currentThread().getName() + "消费,当前容量:" + blockingQueue.size());
				try {
					Thread.sleep(1000);
					blockingQueue.take();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	class Producer implements Runnable {

		private BlockingQueue blockingQueue;

		Producer(BlockingQueue blockingQueue) {
			this.blockingQueue = blockingQueue;
		}

		@Override
		public void run() {
			for (; ; ) {
				System.out.println(Thread.currentThread().getName() + "生产,当前容量:" + blockingQueue.size());
				try {
					Thread.sleep(1000);
					blockingQueue.put("product");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
执行结果

在这里插入图片描述

源码

那么LinkedBlockingQueue与ArrayBlockingQueue之间有什么区别呢?我们先来看看LinkedBlockingQueue吧。

    /**
     * Linked list node class
     */
    static class Node<E> {
        E item;

        /**
         * One of:
         * - the real successor Node
         * - this Node, meaning the successor is head.next
         * - null, meaning there is no successor (this is the last node)
         */
        Node<E> next;

        Node(E x) { item = x; }
    }

    /** The capacity bound, or Integer.MAX_VALUE if none */
    private final int capacity;

    /** Current number of elements */
    private final AtomicInteger count = new AtomicInteger();

    /**
     * Head of linked list.
     * Invariant: head.item == null
     */
    transient Node<E> head;

    /**
     * Tail of linked list.
     * Invariant: last.next == null
     */
    private transient Node<E> last;

相比ArrayBlockingQueue,LinkedBlockingQueue是采用了链表结构,如果不设置capacity,默认大小为Integer.MAX_VALUE。同时维护了队列头部与尾部。另外你发现,维护队列大小的count不再是int,而是AtomicInteger。

    /** Lock held by take, poll, etc */
    private final ReentrantLock takeLock = new ReentrantLock();

    /** Wait queue for waiting takes */
    private final Condition notEmpty = takeLock.newCondition();

    /** Lock held by put, offer, etc */
    private final ReentrantLock putLock = new ReentrantLock();

    /** Wait queue for waiting puts */
    private final Condition notFull = putLock.newCondition();

在并发控制方面,我们看到读和写分别维护了一把锁,实现了锁分离,感觉LinkedBlockingQueue的并发读更高了。
下面来看看是不是这样的:
先看take方法:

    public E take() throws InterruptedException {
        E x;
        int c = -1;
        final AtomicInteger count = this.count;
        final ReentrantLock takeLock = this.takeLock;
        // 获取takeLock锁
        takeLock.lockInterruptibly();
        try {
        	// 当队列为空,take线程阻塞,排在notEmpty阻塞队列中
            while (count.get() == 0) {
                notEmpty.await();
            }
            // 执行出队操作
            x = dequeue();
            // count数 -1,返回的是新增前的值
            c = count.getAndDecrement();
            // 如果个数>1,唤醒因take阻塞的线程去竞争takeLock锁
            if (c > 1)
                notEmpty.signal();
        } finally {
        	// 释放锁
            takeLock.unlock();
        }
        // 在释放takeLock之后判断
        if (c == capacity)
        	// 如果队列已满,唤醒notFull条件队列中的线程执行put操作 (这里有点疑问了,为什么满了才要唤醒put操作呢?)
            signalNotFull();
        return x;
    }
    private E dequeue() {
    	// head 节点是不存元素的
        Node<E> h = head;
        // 出队的是head的下一个first节点
        Node<E> first = h.next;
        h.next = h; // help GC
        // 让head后移一位,取出first元素,并置为null,出队
        head = first;
        E x = first.item;
        first.item = null;
        return x;
    }

    private void signalNotFull() {
        final ReentrantLock putLock = this.putLock;
        putLock.lock();
        try {
            notFull.signal();
        } finally {
            putLock.unlock();
        }
    }

我们先往下看,再看看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锁才能执行put操作
        putLock.lockInterruptibly();
        try {
        	// 当检测到count已满,put现在就阻塞在notFull条件队列中
            while (count.get() == capacity) {
                notFull.await();
            }
            // 否则就入队(这里是没有问题了,因为此时已经获取到了putLock,其他add,offer操作在此时是阻塞的,不会并发执行,所以count只会减小,不会增大,可以入队。另外是从尾部入队,也不会影响出队)
            enqueue(node);
            // 获取最新的count数并加1
            c = count.getAndIncrement();
            if (c + 1 < capacity)
            	// 如果至少还可以放一个元素,则唤醒一个put线程
                notFull.signal();
        } finally {
        	// 释放putLock锁
            putLock.unlock();
        }
        if (c == 0)
        	// 如果元素个数为0,唤醒一个take线程(这里同样是有疑问的,为什么只有为0的时候才唤醒)
            signalNotEmpty();
    }

    private void enqueue(Node<E> node) {
    	// 将node放入链表尾部。首先node置为last的next,放入链表。再将last指向node节点就可以了
        last = last.next = node;
    }

    private void signalNotEmpty() {
        final ReentrantLock takeLock = this.takeLock;
        // 获取takeLock锁,进行唤醒take线程
        takeLock.lock();
        try {
            notEmpty.signal();
        } finally {
            takeLock.unlock();
        }
    }

回答:上述当c==0才唤醒take线程,引用qq_26898645博主的理解,我觉得有道理,贴在这里:

上述put方法中,比较疑惑的地方是,为什么最后要判断,当容量为0时,需要激活notEmpty-Condition阻塞的take线程?

按道理讲,只要进行了put操作,就证明肯定队列不为空了,直接进行signalNotEmpty()不可以吗?

想了想,大概原因是这样:

直接进行signalNotEmpty()可以是可以,不过性能不是最优的,因为如果之前的队列本身就不为空,则说明没有处于因notEmpty.wait()而阻塞的take线程,自然也就无需进行唤醒动作。

另外,判断之前是否有处于阻塞的take线程的方法也非常巧妙,即通过count.getAndIncrement();的返回值获得,
因getAndIncrement返回之前旧值,自然在实现count同步自增的同时,返回了之前值。

小结

1、LinkedBlockingQueue可以指定大小,也可以不指定,不指定大小默认为int最大值。ArrayBlockingQueue必须指定大小。
2、LinkedBlockingQueue的take与put分别持有一把锁,而ArrayBlockingQueue所有操作持有的是一把锁,感觉上LinkedBlockingQueue在并发性能上更好。我测试了一下:

package juc;

import org.junit.Test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ArrayBlockingQueueAndLinkedBlockingQueueTest {

	@Test
	public void test() throws InterruptedException {
		final BlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(10);
		Thread t1 = new Thread(new Taker(arrayBlockingQueue), "arrayBlockingQueueTaker");
		Thread t2 = new Thread(new Puter(arrayBlockingQueue), "arrayBlockingQueuePuter");
		long start1 = System.currentTimeMillis();
		t1.start();
		t2.start();
		t1.join();
		t2.join();
		System.out.println("arrayBlockingQueue消耗时间:" + (System.currentTimeMillis() - start1));

		final BlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(10);
		Thread t3 = new Thread(new Taker(linkedBlockingQueue), "linkedBlockingQueueTaker");
		Thread t4 = new Thread(new Puter(linkedBlockingQueue), "linkedBlockingQueuePuter");
		long start2 = System.currentTimeMillis();
		t3.start();
		t4.start();
		t3.join();
		t4.join();
		System.out.println("linkedBlockingQueue消耗时间:" + (System.currentTimeMillis() - start2));
	}

	class Taker implements Runnable {

		private BlockingQueue blockingQueue;

		Taker(BlockingQueue blockingQueue) {
			this.blockingQueue = blockingQueue;
		}
		@Override
		public void run() {
			for (int i = 0; i < 10000000; i++) {
				try {
					blockingQueue.take();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	class Puter implements Runnable {

		private BlockingQueue blockingQueue;

		Puter(BlockingQueue blockingQueue) {
			this.blockingQueue = blockingQueue;
		}
		@Override
		public void run() {
			for (int i = 0; i < 10000000; i++) {
				try {
					blockingQueue.put("product");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

执行结果:
在这里插入图片描述
执行效率差不多,可能的原因是:虽然LinkedBlockingQueue并发程度较高,但是入队需要新增节点,创建节点对象。而ArrayBlockingQueue可以重用队列存放数组。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值