Java中的阻塞队列(BlockingQueue)

本文深入探讨Java中的阻塞队列(BlockingQueue),包括其基本概念、多种实现方式及应用场景,特别关注ArrayBlockingQueue与LinkedBlockingQueue的内部实现原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文转自:Java中的阻塞队列(BlockingQueue)

什么是阻塞队列

阻塞队列(BlockingQueue)是 Java 5 并发新特性中的内容,阻塞队列的接口是 java.util.concurrent.BlockingQueue,它提供了两个附加操作:当队列中为空时,从队列中获取元素的操作将被阻塞;当队列满时,向队列中添加元素的操作将被阻塞。

阻塞队列常用于生产者和消费者的场景,生产者是往队列里添加元素的线程,消费者是从队列里拿元素的线程。阻塞队列就是生产者存放元素的容器。

阻塞队列提供了四种操作方法:

阻塞队列操作方法

  • 抛出异常:当队列满时,再向队列中插入元素,则会抛出IllegalStateException异常。当队列空时,再向队列中获取元素,则会抛出NoSuchElementException异常。
  • 返回特殊值:当队列满时,向队列中添加元素,则返回false,否则返回true。当队列为空时,向队列中获取元素,则返回null,否则返回元素。
  • 一直阻塞:当阻塞队列满时,如果生产者向队列中插入元素,则队列会一直阻塞当前线程,直到队列可用或响应中断退出。当阻塞队列为空时,如果消费者线程向阻塞队列中获取数据,则队列会一直阻塞当前线程,直到队列空闲或响应中断退出。
  • 超时退出:当队列满时,如果生产线程向队列中添加元素,则队列会阻塞生产线程一段时间,超过指定的时间则退出返回false。当队列为空时,消费线程从队列中移除元素,则队列会阻塞一段时间,如果超过指定时间退出返回null。

Java中的阻塞队列

JDK7提供了7个阻塞队列。分别是

阻塞队列操作方法

下面分别简单介绍一下:

  • ArrayBlockingQueue:是一个用数组实现的有界阻塞队列,此队列按照先进先出(FIFO)的原则对元素进行排序。支持公平锁和非公平锁。【注:每一个线程在获取锁的时候可能都会排队等待,如果在等待时间上,先获取锁的线程的请求一定先被满足,那么这个锁就是公平的。反之,这个锁就是不公平的。公平的获取锁,也就是当前等待时间最长的线程先获取锁】
  • LinkedBlockingQueue:一个由链表结构组成的有界队列,此队列的长度为Integer.MAX_VALUE。此队列按照先进先出的顺序进行排序。
  • PriorityBlockingQueue: 一个支持线程优先级排序的无界队列,默认自然序进行排序,也可以自定义实现compareTo()方法来指定元素排序规则,不能保证同优先级元素的顺序。
  • DelayQueue: 一个实现PriorityBlockingQueue实现延迟获取的无界队列,队列中的元素必须实现Delayed接口,在创建元素时,可以指定多久才能从队列中获取当前元素。只有延时期满后才能从队列中获取元素。(DelayQueue可以运用在以下应用场景:1.缓存系统的设计:可以用DelayQueue保存缓存元素的有效期,使用一个线程循环查询DelayQueue,一旦能从DelayQueue中获取元素时,表示缓存有效期到了。2.定时任务调度。使用DelayQueue保存当天将会执行的任务和执行时间,一旦从DelayQueue中获取到任务就开始执行,从比如TimerQueue就是使用DelayQueue实现的。)
  • SynchronousQueue: 一个不存储元素的阻塞队列,每一个put操作必须等待take操作,否则不能添加元素。支持公平锁和非公平锁。SynchronousQueue的一个使用场景是在线程池里。Executors.newCachedThreadPool()就使用了SynchronousQueue,这个线程池根据需要(新任务到来时)创建新的线程,如果有空闲线程则会重复使用,线程空闲了60秒后会被回收。
  • LinkedTransferQueue: 一个由链表结构组成的无界阻塞队列,相当于其它队列,LinkedTransferQueue队列多了transfer和tryTransfer方法。
  • transfer()方法:如果当前有消费者正在等待接收元素(消费者使用take()方法或带时间限制的poll()方法),transfer()方法可以把生产者传入的元素立刻传输给消费者;如果没有消费者在等待接收元素,transfer()方法会将元素存放到队列的tail节点,并等到该元素被消费者消费了才返回。
  • tryTransfer()方法:该方法是用来试探生产者传入的元素是否能直接传给消费者,如果没有消费者等待接收元素,则返回false。与transfer()方法的区别:tryTransfer()方法是立即返回(无论消费者是否接收),transfer()方法是必须等到消费者消费了才返回。对于带有时间限制的tryTransfer(E e, long timeout, TimeUnit unit)方法,则是试图把生产者传入的元素直接传给消费者,但是如果没有消费者消费该元素则等待指定的时间之后再返回,如果超时还没消费元素,则返回false,如果在超时时间内消费了元素,则返回true。
  • LinkedBlockingDeque:
    LinkedBlockingDeque是一个由链表结构组成的双向阻塞队列。所谓双向队列指的你可以从队列的两端插入和移出元素。双端队列因为多了一个操作队列的入口,在多线程同时入队时,也就减少了一半的竞争。相比其他的阻塞队列,LinkedBlockingDeque多了addFirst,addLast,offerFirst,offerLast,peekFirst,peekLast等方法,以First单词结尾的方法,表示插入,获取(peek)或移除双端队列的第一个元素。以Last单词结尾的方法,表示插入,获取或移除双端队列的最后一个元素。另外插入方法add等同于addLast,移除方法remove等效于removeFirst。但是take方法却等同于takeFirst,不知道是不是Jdk的bug,使用时还是用带有First和Last后缀的方法更清楚。在初始化LinkedBlockingDeque时可以初始化队列的容量,用来防止其再扩容时过渡膨胀。另外双向阻塞队列可以运用在“工作窃取”模式中。

Java中线程安全的内置队列还有两个:ConcurrentLinkedQueue和LinkedTransferQueue,它们使用了CAS这种无锁的方式来实现了线程安全的队列。无锁的方式性能好,但是队列是无界的,用在生产系统中,生产者生产速度过快,可能导致内存溢出。有界的阻塞队列ArrayBlockingQueue和LinkedBlockingQueue,为了减少Java的垃圾回收对系统性能的影响,会尽量选择array/heap格式的数据结构。这样的话就只剩下ArrayBlockingQueue。(先埋个坑在这儿,近来接触到了disruptor,感觉妙不可言。disruptor)

阻塞队列的实现原理

ArrayBlockingQueue的实现原理

构造方法:

ArrayBlockingQueue(int capacity);
ArrayBlockingQueue(int capacity, boolean fair);
ArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c)

ArrayBlockingQueue提供了三种构造方法,参数含义如下:

  • capacity:容量,即队列大小。
  • fair:是否公平锁。
  • c:队列初始化元素,顺序按照Collection遍历顺序。

插入元素:

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();
    }
}

从源码可以看出,生产者首先获得锁lock,然后判断队列是否已经满了,如果满了,则等待,直到被唤醒,然后调用enqueue插入元素。

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();
}

以上是enqueue的实现,实现的操作是插入元素到一个环形数组,然后唤醒notEmpty上阻塞的线程。

获取元素:

public E take() throws InterruptedException {
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        while (count == 0)
            notEmpty.await();
        return dequeue();
    } finally {
        lock.unlock();
    }
}

从源码可以看出,消费者首先获得锁,然后判断队列是否为空,为空,则等待,直到被唤醒,然后调用dequeue获取元素。

private E dequeue() {
    // assert lock.getHoldCount() == 1;
    // assert items[takeIndex] != null;
    final Object[] items = this.items;
    @SuppressWarnings("unchecked")
    E x = (E) items[takeIndex];
    items[takeIndex] = null;
    if (++takeIndex == items.length)
        takeIndex = 0;
    count--;
    if (itrs != null)
        itrs.elementDequeued();
    notFull.signal();
    return x;
}

以上是dequeue的实现,获取环形数组当前takeIndex的元素,并及时将当前元素置为null,设置下一次takeIndex的值takeIndex++,然后唤醒notFull上阻塞的线程。

还有其他方法offer(E e)、poll()、add(E e)、remove()、 offer(E e, long timeout, TimeUnit unit)等的实现,因为常用take和put,这些方法就不一一赘述了。

LinkedBlockingQueue 实现原理

构造方法:

public LinkedBlockingQueue() 
public LinkedBlockingQueue(int capacity) 
public LinkedBlockingQueue(Collection<? extends E> c) 

LinkedBlockingQueue 的默认的容量是Integer.MAX_VALUE,这样的话,如果生产者的速度一旦大于消费者的速度,也许还没有等到队列满阻塞产生,系统内存就有可能已被消耗殆尽了。使用时尽量使用上面的第二个构造函数指定初始容量。

成员变量

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

/** 队列中当前元素数目 */
private final AtomicInteger count = new AtomicInteger();

/** head 节点 */
transient Node<E> head;

/** tail 节点* Invariant: last.next == null */
private transient Node<E> last;

/** 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 中的元素添加时使用 last 节点添加到队列结尾,使用 head 节点从队列前面移除节点,元素读取按照先进先出的顺序。和 ArrayBlockingQueue在put,take操作使用了同一个锁有区别的是,LinkedBlockingQueue使用了不同的锁,put操作和take操作可同时进行,以此来提高整个队列的并发性能。

ArrayBlockingQueue 与 LinkedBlockingQueue 两者对比

  • ArrayBlockingQueue在put,take操作使用了同一个锁,两者操作不能同时进行,而LinkedBlockingQueue使用了不同的锁,put操作和take操作可同时进行。
  • ArrayBlockingQueue和LinkedBlockingQueue间还有一个明显的不同之处在于,前者在插入或删除元素时不会产生或销毁任何额外的对象实例,而后者则会生成一个额外的Node对象,这在长时间内需要高效并发地处理大批量数据的系统中,其对于GC的影响还是存在一定的区别。

插入元素

	/**
     * Inserts the specified element at the tail of this queue, waiting if
     * necessary for space to become available.
     */
    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();
    }

    /**
     * Signals a waiting take. Called only from put/offer (which do not
     * otherwise ordinarily lock takeLock.)
     */
    private void signalNotEmpty() {
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lock();
        try {
            notEmpty.signal();
        } finally {
            takeLock.unlock();
        }
    }

    private void enqueue(Node<E> node) {
        // 将新的节点添加到队列末尾
        last = last.next = node;
    }

读取元素:

	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();
        }       
        // 如果移除元素后,元素数目刚好比容量数小1,则通知等待的写入锁
        if (c == capacity)
            signalNotFull();
        return x;
    }

    private E dequeue() {
        // 使 head 节点替换第一个节点
        Node<E> h = head;
        Node<E> first = h.next;
        h.next = h; // help GC
        head = first;
        E x = first.item;
        first.item = null;
        return x;
    }

drainTo():
一次性从BlockingQueue获取所有可用的数据对象(还可以指定获取数据的个数), 通过该方法,可以提升获取数据效率;不需要多次分批加锁或释放锁。

	public int drainTo(Collection<? super E> c) {
        return drainTo(c, Integer.MAX_VALUE);
    }

	public int drainTo(Collection<? super E> c, int maxElements) {
        if (c == null)
            throw new NullPointerException();
        if (c == this)
            throw new IllegalArgumentException();
        if (maxElements <= 0)
            return 0;
        boolean signalNotFull = false;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lock();
        try {
            int n = Math.min(maxElements, count.get());
            // count.get provides visibility to first n Nodes
            Node<E> h = head;
            int i = 0;
            try {
                while (i < n) {
                    Node<E> p = h.next;
                    c.add(p.item);
                    p.item = null;
                    h.next = h;
                    h = p;
                    ++i;
                }
                return n;
            } finally {
                // Restore invariants even if c.add() threw
                if (i > 0) {
                    // assert h.item == null;
                    head = h;
                    signalNotFull = (count.getAndAdd(-i) == capacity);
                }
            }
        } finally {
            takeLock.unlock();
            if (signalNotFull)
                signalNotFull();
        }
    }

阻塞队列的基本使用

ArrayBlockingQueue 的使用

使用阻塞队列实现生产者-消费者模式:

/**
 * Created by noly on 2017/5/19.
 */
public class BlockingQueueTest {

    public static void main (String[] args) {
        ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10);

        Consumer consumer = new Consumer(queue);
        Producer producer = new Producer(queue);

        producer.start();
        consumer.start();
    }
}

class Consumer extends Thread {
    private ArrayBlockingQueue<Integer> queue;
    public Consumer(ArrayBlockingQueue<Integer> queue){
        this.queue = queue;
    }
    @Override
    public void run() {
        while(true) {
            try {
                Integer i = queue.take();
                System.out.println("消费者从队列取出元素:" + i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Producer extends Thread {
    private ArrayBlockingQueue<Integer> queue;
    public Producer(ArrayBlockingQueue<Integer> queue){
        this.queue = queue;
    }
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            try {
                queue.put(i);
                System.out.println("生产者向队列插入元素:" + i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

如果不使用阻塞队列,使用Object.wait()和Object.notify()、非阻塞队列实现生产者-消费者模式,考虑线程间的通讯,会非常麻烦。

DelayQueue 的应用

实体类

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

// 必须实现Delayed接口
public class Student implements Delayed {

	private String name;
	private long submitTime; // 交卷时间(开始考试时间默认为实例初始化时间)
	private long workTime;	 // 考试时长
	
	public Student(String name, long workTime) {
		this.name = name;
		this.workTime = workTime;
		this.submitTime = TimeUnit.NANOSECONDS.convert(workTime, TimeUnit.MILLISECONDS) + System.nanoTime();
		
		System.out.println(this.name + " 交卷,用时" + workTime);
	}

	public String getName() {
        return this.name + " 交卷,用时" + workTime;
    }
	
	// 重写 compareTo 方法,用于指定元素的顺序
	@Override
	public int compareTo(Delayed o) {
		Student that = (Student) o;
		return submitTime > that.submitTime ? 1 : (submitTime < that.submitTime ? -1 : 0);
	}

	// 必须实现该方法
	@Override
	public long getDelay(TimeUnit unit) {
		//返回一个延迟时间 (这里是 考试所用时间)
		return unit.convert(submitTime - System.nanoTime(), TimeUnit.NANOSECONDS);
	}

}

测试

import java.util.concurrent.DelayQueue;

public class DelayQueueTest {

	public static void main(String[] args) throws InterruptedException {
		// 新建一个等待队列
		final DelayQueue<Student> bq = new DelayQueue<Student>();
		Student stu;
		for (int i = 0; i < 5; i++) {
			stu = new Student("学生" + i, Math.round(Math.random() * 1000 + i));
			bq.put(stu); // 将数据存到队列里!
		}	
				
		long start = System.currentTimeMillis();
		System.out.println(bq.take().getName());
		System.out.println(System.currentTimeMillis() - start);
	}
}

执行结果:

学生0 交卷,用时602
学生1 交卷,用时174
学生2 交卷,用时499
学生3 交卷,用时580
学生4 交卷,用时464
学生1 交卷,用时174
176

参考资料:

聊聊并发(七)——Java中的阻塞队列

阻塞队列和ArrayBlockingQueue源码解析

高性能队列——Disruptor

<think>我们被要求提供关于JavaBlockingQueue的使用指南、原理和示例。根据引用内容,我们知道BlockingQueueJava并发包中的一个重要接口,它支持在队列满或空时阻塞等待的线程安全操作。我们将从以下几个方面展开: 1. BlockingQueue的核心概念 2. 主要实现类:ArrayBlockingQueue和LinkedBlockingQueue 3. 核心方法解析(添加元素和检索元素的方法) 4. 使用场景和示例 5. 原理剖析 6. 常见问题解答 下面开始详细回答。 --- ### 一、BlockingQueue 核心概念 BlockingQueue 是一个支持两个附加操作的队列:当队列为空时,从队列中获取元素的操作将被阻塞,直到队列非空;当队列满时,向队列中添加元素的操作将被阻塞,直到队列不满。这种特性使得 BlockingQueue 成为多线程编程中实现生产者-消费者模式的理想选择[^1]。 **核心特性**: - **线程安全**:所有实现类都是线程安全的。 - **阻塞操作**:提供阻塞插入和移除方法。 - **容量限制**:可以是有界队列(固定容量)或无界队列(容量为Integer.MAX_VALUE)。 --- ### 二、主要实现类 Java 提供了多种 BlockingQueue 的实现,最常用的是: 1. **ArrayBlockingQueue**: - 基于数组的有界阻塞队列。 - 按照FIFO(先进先出)原则排序元素。 - 创建时必须指定容量,且不可变。 - 支持公平锁策略(可选的构造函数参数),避免线程饥饿。 2. **LinkedBlockingQueue**: - 基于链表的可选有界阻塞队列。 - 默认容量为 `Integer.MAX_VALUE`(近似无界),也可在创建时指定容量。 - 按照FIFO原则排序元素。 - 通常比数组队列有更高的吞吐量,但在高并发下性能更不可预测。 其他实现包括 `PriorityBlockingQueue`(支持优先级排序)、`SynchronousQueue`(不存储元素,每个插入操作必须等待一个移除操作)、`DelayQueue`(元素只有在延迟期满后才能被取出)等[^1][^3]。 --- ### 三、核心方法解析 BlockingQueue 接口定义了四组不同的方法用于插入、移除和检查元素,每组方法在队列满或空时行为不同[^2]: #### 1. 添加元素 | 方法 | 行为 | |------|------| | `add(e)` | 如果队列未满,立即插入并返回`true`;否则抛出`IllegalStateException` | | `put(e)` | 如果队列满,则阻塞直到队列有空闲空间再插入 | | `offer(e)` | 如果队列未满,插入并返回`true`;否则返回`false` | | `offer(e, timeout, unit)` | 在指定的超时时间内尝试插入,成功返回`true`,超时返回`false` | #### 2. 移除元素 | 方法 | 行为 | |------|------| | `remove()` | 移除并返回队列头元素,如果队列为空则抛出`NoSuchElementException` | | `take()` | 如果队列为空,则阻塞直到队列有元素可用 | | `poll()` | 移除并返回队列头元素,队列为空时返回`null` | | `poll(timeout, unit)` | 在指定的超时时间内尝试移除并返回队列头元素,超时返回`null` | #### 3. 检查元素 | 方法 | 行为 | |------|------| | `element()` | 返回队列头元素(不移除),队列为空时抛出`NoSuchElementException` | | `peek()` | 返回队列头元素(不移除),队列为空时返回`null` | > **注意**:这些方法中,`put()` 和 `take()` 是阻塞操作的核心方法。 --- ### 四、使用场景和示例 #### 场景:生产者-消费者模型 生产者线程生成数据并放入队列,消费者线程从队列中取出数据并处理。 ```java import java.util.concurrent.*; public class ProducerConsumerExample { private static final int QUEUE_CAPACITY = 5; private static final BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(QUEUE_CAPACITY); public static void main(String[] args) { // 创建线程池 ExecutorService executor = Executors.newFixedThreadPool(2); // 生产者任务 Runnable producer = () -> { try { for (int i = 0; i < 10; i++) { queue.put(i); // 阻塞直到队列有空位 System.out.println("生产: " + i); Thread.sleep(100); // 模拟生产耗时 } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }; // 消费者任务 Runnable consumer = () -> { try { for (int i = 0; i < 10; i++) { Integer item = queue.take(); // 阻塞直到队列有元素 System.out.println("消费: " + item); Thread.sleep(200); // 模拟消费耗时 } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }; // 启动生产者和消费者 executor.submit(producer); executor.submit(consumer); // 关闭线程池 executor.shutdown(); } } ``` #### 场景:任务提交与超时控制 使用 `offer(e, timeout, unit)` 和 `poll(timeout, unit)` 实现带超时的任务提交。 ```java public class TimeoutExample { public static void main(String[] args) throws InterruptedException { BlockingQueue<String> queue = new LinkedBlockingQueue<>(1); // 生产者:尝试在2秒内插入元素 new Thread(() -> { try { boolean success = queue.offer("data", 2, TimeUnit.SECONDS); System.out.println("插入结果: " + success); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); // 消费者:等待1秒后取走元素 Thread.sleep(1000); String data = queue.poll(1, TimeUnit.SECONDS); System.out.println("取出数据: " + data); } } ``` --- ### 五、原理剖析 #### 1. 锁与条件变量 BlockingQueue 的实现通常依赖于**可重入锁(ReentrantLock)**和**条件变量(Condition)**。以 `ArrayBlockingQueue` 为例: ```java // ArrayBlockingQueue 内部实现关键代码 final ReentrantLock lock; private final Condition notEmpty; // 等待非空的条件 private final Condition notFull; // 等待非满的条件 public ArrayBlockingQueue(int capacity, boolean fair) { // ... 初始化数组 lock = new ReentrantLock(fair); notEmpty = lock.newCondition(); notFull = lock.newCondition(); } // put 方法实现 public void put(E e) throws InterruptedException { Objects.requireNonNull(e); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (count == items.length) // 队列满 notFull.await(); // 等待非满条件 enqueue(e); // 入队 } finally { lock.unlock(); } } // take 方法实现 public E take() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (count == 0) // 队列空 notEmpty.await(); // 等待非空条件 return dequeue(); // 出队 } finally { lock.unlock(); } } // 入队后唤醒等待非空的线程 private void enqueue(E x) { // ... 入队操作 notEmpty.signal(); // 唤醒一个等待非空的线程 } // 出队后唤醒等待非满的线程 private E dequeue() { // ... 出队操作 notFull.signal(); // 唤醒一个等待非满的线程 return x; } ``` #### 2. 无界队列实现 `LinkedBlockingQueue` 默认无界(实际容量为`Integer.MAX_VALUE`),其内部使用两个锁(`putLock` 和 `takeLock`)分别控制入队和出队操作,减少锁竞争,提高并发性能[^3]。 --- ### 六、常见问题解答 #### ❌ 问题1:如何选择 ArrayBlockingQueue 和 LinkedBlockingQueue? - **ArrayBlockingQueue**:固定大小,内存连续,性能稳定,适合已知固定容量的场景。 - **LinkedBlockingQueue**:默认无界(可指定容量),适合任务量未知但可能很大的场景,但注意可能引起内存溢出。 #### ❌ 问题2:阻塞队列在什么情况下会导致线程饥饿? - 使用非公平锁(默认)时,新来的线程可能比等待的线程先获取锁,导致等待线程饥饿。 - **解决方案**:创建队列时指定公平锁(如 `new ArrayBlockingQueue<>(100, true)`),但会降低吞吐量。 #### ❌ 问题3:如何处理阻塞队列中的任务拒绝? 当使用有界队列时,生产者可能因队列满而被阻塞或拒绝。常见处理策略: - 使用 `offer(e, timeout, unit)` 设置超时,超时后记录日志或执行降级逻辑。 - 结合线程池的 `RejectedExecutionHandler` 处理(如丢弃任务、调用者运行等)[^3]。 --- ### 七、最佳实践 1. **合理设置队列容量**: - 过小:频繁阻塞,降低吞吐。 - 过大:占用内存,可能引发OOM(无界队列风险)。 2. **优先使用标准库提供的线程池**: ```java // 使用 ThreadPoolExecutor 并指定阻塞队列 ExecutorService executor = new ThreadPoolExecutor( 4, // 核心线程数 8, // 最大线程数 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100) // 使用有界队列 ); ``` 3. **监控队列状态**: ```java // 定期打印队列大小 ScheduledExecutorService monitor = Executors.newSingleThreadScheduledExecutor(); monitor.scheduleAtFixedRate(() -> System.out.println("队列大小: " + queue.size()), 0, 1, TimeUnit.SECONDS ); ``` --- 通过以上解析,我们深入了解了 BlockingQueue 的使用方法、内部原理及实践技巧。合理使用阻塞队列可以显著简化多线程编程,提高系统稳定性和性能[^1][^3]。 §§相关问题§§ 1. 如何监控 BlockingQueue 的性能指标(如队列长度、等待时间)? 2. BlockingQueue 在分布式系统中如何应用? 3. 如何实现一个自定义的 BlockingQueue? 4. BlockingQueue 与消息队列(如Kafka、RabbitMQ)有何异同?</think>### Java BlockingQueue 使用指南、原理与示例 BlockingQueueJava 并发包 (`java.util.concurrent`) 中的核心接口,专为生产者-消费者模型设计,支持线程安全的阻塞操作。以下是全面解析: --- #### 一、BlockingQueue 核心原理 | 特性 | 说明 | |---------------------|----------------------------------------------------------------------| | **阻塞机制** | 队列满时插入操作阻塞,队列空时获取操作阻塞[^1] | | **线程安全** | 内置锁(ReentrantLock)和条件变量(Condition)实现线程同步 | | **容量策略** | 分为有界队列(如 ArrayBlockingQueue)和无界队列(如 LinkedBlockingQueue) | | **FIFO 原则** | 默认先进先出(PriorityBlockingQueue 支持优先级排序) | **底层实现关键**: ```java // ArrayBlockingQueue 核心锁机制 final ReentrantLock lock; private final Condition notEmpty; // 非空条件 private final Condition notFull; // 非满条件 public void put(E e) throws InterruptedException { Objects.requireNonNull(e); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (count == items.length) // 队列满时阻塞 notFull.await(); enqueue(e); // 入队并唤醒消费者 } finally { lock.unlock(); } } ``` --- #### 二、核心方法对比 ##### 1. 添加元素方法 | 方法 | 队列满时行为 | 返回值 | |-------------------------------|----------------------------------------|----------------------| | `add(e)` | 抛出 `IllegalStateException` | `boolean` (成功/失败)| | `put(e)` | 阻塞直到有空位 | `void` | | `offer(e)` | 立即返回失败 | `boolean` | | `offer(e, timeout, timeUnit)` | 阻塞等待指定时间,超时返回失败 | `boolean` | ##### 2. 获取元素方法 | 方法 | 队列空时行为 | 返回值 | |-------------------------------|----------------------------------------|-------------| | `remove()` | 抛出 `NoSuchElementException` | `E` | | `take()` | 阻塞直到有元素 | `E` | | `poll()` | 立即返回 `null` | `E` 或 `null`| | `poll(timeout, timeUnit)` | 阻塞等待指定时间,超时返回 `null` | `E` 或 `null`| | `peek()` | 返回队首元素(不删除) | `E` 或 `null`| [^2] --- #### 三、常用实现类对比 | 实现类 | 数据结构 | 是否有界 | 锁机制 | 适用场景 | |-----------------------|----------------|----------|----------------------|------------------------------| | `ArrayBlockingQueue` | 数组 | 有界 | 单锁(生产消费共用) | 固定大小队列,内存可控 | | `LinkedBlockingQueue` | 链表 | 可选有界 | 双锁(分离生产消费) | 高吞吐量场景[^1] | | `PriorityBlockingQueue` | 二叉堆 | 无界 | 单锁 | 按优先级处理任务 | | `SynchronousQueue` | 无容器 | 特殊 | CAS 操作 | 直接传递任务(生产者等消费者)| --- #### 四、实战代码示例 ##### 1. 生产者-消费者模型(ArrayBlockingQueue) ```java import java.util.concurrent.*; public class ProducerConsumerExample { private static final int CAPACITY = 3; private static final BlockingQueue<String> queue = new ArrayBlockingQueue<>(CAPACITY); public static void main(String[] args) { // 生产者 Runnable producer = () -> { try { for (int i = 0; i < 5; i++) { String item = "Item-" + i; queue.put(item); // 队列满时阻塞 System.out.println("生产: " + item); Thread.sleep(200); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }; // 消费者 Runnable consumer = () -> { try { for (int i = 0; i < 5; i++) { String item = queue.take(); // 队列空时阻塞 System.out.println("消费: " + item); Thread.sleep(500); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }; new Thread(producer).start(); new Thread(consumer).start(); } } ``` ##### 2. 超时控制(offer/poll) ```java // 生产者线程 if (!queue.offer("Urgent", 2, TimeUnit.SECONDS)) { System.err.println("插入失败:队列已满超时"); } // 消费者线程 String item = queue.poll(3, TimeUnit.SECONDS); if (item == null) { System.out.println("获取失败:队列空超时"); } ``` ##### 3. 优先级队列(PriorityBlockingQueue) ```java BlockingQueue<Task> priorityQueue = new PriorityBlockingQueue<>( 10, Comparator.comparingInt(Task::getPriority) ); // 添加任务 priorityQueue.put(new Task("Low", 1)); priorityQueue.put(new Task("High", 3)); Task next = priorityQueue.take(); // 先取出优先级3的任务 ``` --- #### 五、典型应用场景 1. **线程池任务调度** ```java // ThreadPoolExecutor 内部使用 BlockingQueue ExecutorService executor = new ThreadPoolExecutor( 2, 4, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100) // 任务队列 ); ``` 2. **高并发请求缓冲** ```java // 处理突发流量 BlockingQueue<HttpRequest> buffer = new LinkedBlockingQueue<>(1000); ``` 3. **多阶段数据处理流水线** ```java // 阶段1 → 阶段2 传递数据 BlockingQueue<Data> stage1Output = new LinkedBlockingQueue<>(); BlockingQueue<Result> stage2Output = new ArrayBlockingQueue<>(50); ``` --- #### 六、常见问题解决方案 ##### ❌ 问题1:队列满导致生产者阻塞 **解决方案**: - 使用 `offer(e, timeout, unit)` 设置超时 - 启用拒绝策略(如丢弃旧元素) ```java // 自定义拒绝策略:丢弃最旧元素 BlockingQueue<String> queue = new LinkedBlockingQueue<>(10) { @Override public boolean offer(String e) { if (size() == capacity) poll(); // 丢弃队首 return super.offer(e); } }; ``` ##### ❌ 问题2:消费者处理速度慢拖垮系统 **解决方案**: - 限制队列容量(避免无界队列 OOM) - 监控队列长度并告警 ```java if (queue.size() > WARNING_THRESHOLD) { log.warn("队列积压: {}/{}", queue.size(), queue.remainingCapacity()); } ``` ##### ❌ 问题3:线程阻塞导致无法退出 **解决方案**: - 使用中断机制唤醒线程 ```java // 消费者线程退出逻辑 while (!Thread.currentThread().isInterrupted()) { String item = queue.poll(1, TimeUnit.SECONDS); if (item != null) process(item); } ``` --- #### 七、性能优化建议 1. **选择合适队列类型** - 高吞吐 → `LinkedBlockingQueue`(分离锁提升并发) - 低延迟 → `ArrayBlockingQueue`(内存连续访问快) 2. **避免无界队列** - 防止内存溢出(OOM),尤其在高并发场景[^4] 3. **监控关键指标** ```java // 监控队列负载 double loadFactor = (double) queue.size() / queue.remainingCapacity(); ``` --- 通过合理使用 BlockingQueue,可有效解决线程间数据传递的同步问题,构建高效稳定的并发系统[^1][^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值