【从入门到放弃-Java】并发编程-JUC-LinkedBlockingQueue

简介: 简介 上一篇【从入门到放弃-Java】并发编程-JUC-ConcurrentLinkedQueue学习了并发队列ConcurrentLinkedQueue,它是一个非阻塞无界队列。本文来学习下JUC中的一个阻塞有界队列-LinkedBlockingQueue。

简介

上一篇【从入门到放弃-Java】并发编程-JUC-ConcurrentLinkedQueue学习了并发队列ConcurrentLinkedQueue,它是一个非阻塞无界队列。本文来学习下JUC中的一个阻塞有界队列-LinkedBlockingQueue。

LinkedBlockingQueue


如图继承了AbstractQueue类,实现了BlockingQueue和Serializable接口

LinkedBlockingQueue

/**
 * Creates a {@code LinkedBlockingQueue} with a capacity of
 * {@link Integer#MAX_VALUE}.
 */
// 如果没传capacity 则默认使用Integer.MAX_VALUE作为队列大小
public LinkedBlockingQueue() {
    this(Integer.MAX_VALUE);
}

/**
 * Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity.
 *
 * @param capacity the capacity of this queue
 * @throws IllegalArgumentException if {@code capacity} is not greater
 *         than zero
 */
 //设置大小为capacity,并设置item为null的head和last辅助节点
public LinkedBlockingQueue(int capacity) {
    if (capacity <= 0) throw new IllegalArgumentException();
    this.capacity = capacity;
    last = head = new Node<E>(null);
}

/**
 * Creates a {@code LinkedBlockingQueue} with a capacity of
 * {@link Integer#MAX_VALUE}, initially containing the elements of the
 * given collection,
 * added in traversal order of the collection's iterator.
 *
 * @param c the collection of elements to initially contain
 * @throws NullPointerException if the specified collection or any
 *         of its elements are null
 */
public LinkedBlockingQueue(Collection<? extends E> c) {
    this(Integer.MAX_VALUE);
    //加入队锁
    final ReentrantLock putLock = this.putLock;
    putLock.lock(); // Never contended, but necessary for visibility
    try {
        int n = 0;
        //依次将Collection中的元素加入队列
        for (E e : c) {
            if (e == null)
                throw new NullPointerException();
            if (n == capacity)
                throw new IllegalStateException("Queue full");
            enqueue(new Node<E>(e));
            ++n;
        }
        //设置队列大小n
        count.set(n);
    } finally {
        //解锁
        putLock.unlock();
    }
}

可以从构造函数看出,LinkedBlockingQueue是一个有界的队列,队列最大值为capacity,如果初始化时不设置队列大小,则默认大小为Integer.MAX_VALUE

put

将元素加入队列,如果队列满,则一直等待,直到线程被中断或被唤醒

/**
 * Inserts the specified element at the tail of this queue, waiting if
 * necessary for space to become available.
 *
 * @throws InterruptedException {@inheritDoc}
 * @throws NullPointerException {@inheritDoc}
 */
public void put(E e) throws InterruptedException {
    if (e == null) throw new NullPointerException();
    final int c;
    final Node<E> node = new Node<E>(e);
    
    //入队锁,如果收到中断信号,则抛出异常
    final ReentrantLock putLock = this.putLock;
    final AtomicInteger count = this.count;
    putLock.lockInterruptibly();
    try {
        /*
         * Note that count is used in wait guard even though it is
         * not protected by lock. This works because count can
         * only decrease at this point (all other puts are shut
         * out by lock), and we (or some other waiting put) are
         * signalled if it ever changes from capacity. Similarly
         * for all other uses of count in other wait guards.
         */
         //如果队列满了,则通知线程进入await状态。
        while (count.get() == capacity) {
            notFull.await();
        }
        //将node加入队列
        enqueue(node);
        //队列元素数加一
        c = count.getAndIncrement();
        //如果队列没满,则唤醒await的线程进行入队操作
        if (c + 1 < capacity)
            notFull.signal();
    } finally {
        //释放锁
        putLock.unlock();
    }
    //如果是第一次添加元素,则通知等待的读线程可以开始读数据了
    if (c == 0)
        signalNotEmpty();
}

offer

/**
 * 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.
 * When using a capacity-restricted queue, this method is generally
 * preferable to method {@link BlockingQueue#add add}, which can fail to
 * insert an element only by throwing an exception.
 *
 * @throws NullPointerException if the specified element is null
 */
 //将元素加入队列,如果队列满,则直接返回false
public boolean offer(E e) {
    if (e == null) throw new NullPointerException();
    final AtomicInteger count = this.count;
    //如果队列满了,则直接返回false。
    if (count.get() == capacity)
        return false;
    final int c;
    final Node<E> node = new Node<E>(e);
    //加入队锁
    final ReentrantLock putLock = this.putLock;
    putLock.lock();
    try {
        //再次判断, 队列是否满了,避免在第一次判断后和加锁前,队列被加满
        if (count.get() == capacity)
            return false;
        //将node添加到队列中
        enqueue(node);
        c = count.getAndIncrement();
        //如果队列没满,则唤醒await的线程进行入队操作
        if (c + 1 < capacity)
            notFull.signal();
    } finally {
        //释放锁
        putLock.unlock();
    }
    //如果是第一次添加元素,则通知等待的读线程可以开始读数据了
    if (c == 0)
        signalNotEmpty();
    return true;
}

/**
 * Inserts the specified element at the tail of this queue, waiting if
 * necessary up to the specified wait time for space to become available.
 *
 * @return {@code true} if successful, or {@code false} if
 *         the specified waiting time elapses before space is available
 * @throws InterruptedException {@inheritDoc}
 * @throws NullPointerException {@inheritDoc}
 */
 //将元素加入队列,可以设置等待超时时间,如果队列满,则等待timeout毫秒,超时返回false
public boolean offer(E e, long timeout, TimeUnit unit)
    throws InterruptedException {

    if (e == null) throw new NullPointerException();
    long nanos = unit.toNanos(timeout);
    final int c;
    //加锁
    final ReentrantLock putLock = this.putLock;
    final AtomicInteger count = this.count;
    putLock.lockInterruptibly();
    try {
        //如果队列满了,则等待timeout毫秒,超时则返回false
        while (count.get() == capacity) {
            if (nanos <= 0L)
                return false;
            nanos = notFull.awaitNanos(nanos);
        }
        //入队
        enqueue(new Node<E>(e));
        c = count.getAndIncrement();
        //如果队列没满,则唤醒await的线程进行入队操作
        if (c + 1 < capacity)
            notFull.signal();
    } finally {
        //释放锁
        putLock.unlock();
    }
    //如果是第一次添加元素,则通知等待的读线程可以开始读数据了
    if (c == 0)
        signalNotEmpty();
    return true;
}

take

从队列中取出元素,如果队列为空,则一直等待,直到线程被中断或被唤醒

public E take() throws InterruptedException {
    final E x;
    final int c;
    final AtomicInteger count = this.count;
    //加出队锁
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
        //如果队列为空,则通知线程进入await状态。
        while (count.get() == 0) {
            notEmpty.await();
        }
        //从队列头部取出元素
        x = dequeue();
        //count减一
        c = count.getAndDecrement();
        //如果队列不为空,则唤醒出队等待线程
        if (c > 1)
            notEmpty.signal();
    } finally {
        //释放锁
        takeLock.unlock();
    }
    //如果从满的队列中出列,则唤醒入队线程,队列已经不满了,可以添加元素了
    if (c == capacity)
        signalNotFull();
    return x;
}

poll

public E poll() {
    final AtomicInteger count = this.count;
    //如果队列为空,直接返回null
    if (count.get() == 0)
        return null;
    final E x;
    final int c;
    //加出队锁
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        //如果队列为空,直接返回null
        if (count.get() == 0)
            return null;
        //出队,移除第一个数据节点
        x = dequeue();
        c = count.getAndDecrement();
        //如果队列不为空,则唤醒出队等待线程
        if (c > 1)
            notEmpty.signal();
    } finally {
        takeLock.unlock();
    }
    //如果从满的队列中出列,则唤醒入队线程,队列已经不满了,可以添加元素了
    if (c == capacity)
        signalNotFull();
    return x;
}
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
        final E x;
        final int c;
        long nanos = unit.toNanos(timeout);
        final AtomicInteger count = this.count;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lockInterruptibly();
        try {
            //如果队列为空,则等待timeout时间, 超时返回null
            while (count.get() == 0) {
                if (nanos <= 0L)
                    return null;
                nanos = notEmpty.awaitNanos(nanos);
            }
            //出队列
            x = dequeue();
            c = count.getAndDecrement();
            //如果队列不为空,则唤醒出队等待线程
            if (c > 1)
                notEmpty.signal();
        } finally {
            //释放锁
            takeLock.unlock();
        }
        //如果从满的队列中出列,则唤醒入队线程,队列已经不满了,可以添加元素了
        if (c == capacity)
            signalNotFull();
        return x;
    }

peek

public E peek() {
    final AtomicInteger count = this.count;
    //如果队列为空,返回null
    if (count.get() == 0)
        return null;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        //队列不为空返回第一个数据节点的元素,不移除节点,为空则返回null
        return (count.get() > 0) ? head.next.item : null;
    } finally {
        takeLock.unlock();
    }
}

remove

/**
 * Removes a single instance of the specified element from this queue,
 * if it is present.  More formally, removes an element {@code e} such
 * that {@code o.equals(e)}, if this queue contains one or more such
 * elements.
 * Returns {@code true} if this queue contained the specified element
 * (or equivalently, if this queue changed as a result of the call).
 *
 * @param o element to be removed from this queue, if present
 * @return {@code true} if this queue changed as a result of the call
 */
public boolean remove(Object o) {
    //如果移除的元素为null,在返回false
    if (o == null) return false;
    //加入队和出队锁
    fullyLock();
    try {
        //遍历队列,存在元素o则移除,返回true,否则返回false
        for (Node<E> pred = head, p = pred.next;
             p != null;
             pred = p, p = p.next) {
            if (o.equals(p.item)) {
                unlink(p, pred);
                return true;
            }
        }
        return false;
    } finally {
        //释放入队锁和出队锁
        fullyUnlock();
    }
}

add、element

都在AbstractQueue中实现,上文【从入门到放弃-Java】并发编程-JUC-ConcurrentLinkedQueue中已分析,不再赘述。

总结

通过源码分析,我们了解到了入队和出队的机制。初始化时,需要设置队列大小, 在队列满时,入队操作会等待,队列为空时,出队操作会等待。即LinkedBlockingQueue是一个有界的阻塞队列。
和ConcurrentLinkedQueue对比,LinkedBlockingQueue采用锁分离,比较适合生产和消费频率差不多的场景,并且锁同步更适合单消费者的任务队列,而ConcurrentLinkedQueue使用CAS,并发性能较高更适合消费者多的消息队列。
在常用线程池中,Executors.newFixedThreadPool也是采用的LinkedBlockingQueue作为workQueue,在线程数超过corePoolSize后,会将任务加入到workQueue中等待处理。关于线程池的使用,后面会详细展开。

更多文章

见我的博客:https://nc2era.com

written by AloofJr,转载请注明出处

目录
相关文章
|
12天前
|
安全 Java 数据库连接
2025 年最新 Java 学习路线图含实操指南助你高效入门 Java 编程掌握核心技能
2025年最新Java学习路线图,涵盖基础环境搭建、核心特性(如密封类、虚拟线程)、模块化开发、响应式编程、主流框架(Spring Boot 3、Spring Security 6)、数据库操作(JPA + Hibernate 6)及微服务实战,助你掌握企业级开发技能。
124 3
|
3月前
|
IDE Java 数据挖掘
Java 基础类从入门到精通实操指南
这份指南专注于**Java 17+**的新特性和基础类库的现代化用法,涵盖开发环境配置、数据类型增强(如文本块)、字符串与集合处理进阶、异常改进(如密封类)、IO操作及实战案例。通过具体代码示例,如CSV数据分析工具,帮助开发者掌握高效编程技巧。同时提供性能优化建议和常用第三方库推荐,适合从入门到精通的Java学习者。资源链接:[点此下载](https://pan.quark.cn/s/14fcf913bae6)。
169 35
|
3月前
|
Java API 微服务
2025 年 Java 从入门到精通学习笔记全新版
《Java学习笔记:从入门到精通(2025更新版)》是一本全面覆盖Java开发核心技能的指南,适合零基础到高级开发者。内容包括Java基础(如开发环境配置、核心语法增强)、面向对象编程(密封类、接口增强)、进阶技术(虚拟线程、结构化并发、向量API)、实用类库与框架(HTTP客户端、Spring Boot)、微服务与云原生(容器化、Kubernetes)、响应式编程(Reactor、WebFlux)、函数式编程(Stream API)、测试技术(JUnit 5、Mockito)、数据持久化(JPA、R2DBC)以及实战项目(Todo应用)。
190 5
|
3月前
|
监控 Java 测试技术
2025 年 Java 核心技术从入门到精通实战指南
《2025年Java核心技术实战指南》全面覆盖Java开发的最新趋势与最佳实践。内容包括Java新特性(如模式匹配、文本块、记录类)、微服务架构(Spring Boot 3.0+、Spring Cloud)、响应式编程(Reactor、WebFlux)、容器化与云原生(Docker、Kubernetes)、数据访问技术(JPA、R2DBC)、函数式编程、单元测试与集成测试(JUnit 5、Mockito)、性能优化与监控等。通过实战案例,帮助开发者掌握构建高性能、高可用系统的技能。代码资源可从[链接](https://pan.quark.cn/s/14fcf913bae6)获取。
191 7
|
3月前
|
消息中间件 Java 微服务
2025 版 Java 学习路线实战指南从入门到精通
《Java学习路线实战指南(2025版)》是一份全面的Java开发学习手册,涵盖基础环境搭建、核心语法与新特性、数据结构与算法、微服务架构、云原生技术栈、AI融合及项目实战。内容包括JDK安装配置、IntelliJ IDEA设置、Records类与模式匹配增强、LeetCode题解、Spring Cloud微服务开发、Kubernetes部署、OpenAI API调用等。结合在线商城系统案例,采用Vue 3、Spring Boot 3.5、MySQL、Elasticsearch等技术,提供从理论到实践的完整路径,助力开发者掌握2025年最新趋势与最佳实践。
298 4
|
21天前
|
前端开发 Java 数据库
Java 项目实战从入门到精通 :Java Web 在线商城项目开发指南
本文介绍了一个基于Java Web的在线商城项目,涵盖技术方案与应用实例。项目采用Spring、Spring MVC和MyBatis框架,结合MySQL数据库,实现商品展示、购物车、用户注册登录等核心功能。通过Spring Boot快速搭建项目结构,使用JPA进行数据持久化,并通过Thymeleaf模板展示页面。项目结构清晰,适合Java Web初学者学习与拓展。
137 1
|
14天前
|
算法 Java 测试技术
零基础学 Java: 从语法入门到企业级项目实战的详细学习路线解析
本文为零基础学习者提供完整的Java学习路线,涵盖语法基础、面向对象编程、数据结构与算法、多线程、JVM原理、Spring框架、Spring Boot及项目实战,助你从入门到进阶,系统掌握Java编程技能,提升实战开发能力。
58 0
|
2月前
|
存储 缓存 NoSQL
java 集合入门基础理论的核心概念与实用长尾知识
本文介绍了Java集合框架的基础理论知识,包括单列集合(List、Set、Queue)和双列集合(Map)的特点及常用实现类(如ArrayList、HashSet、HashMap等)。详细讲解了集合的遍历方式(迭代器、增强for循环、Lambda表达式)和典型应用场景(如数据去重、键值存储等)。通过具体代码示例,帮助初学者理解集合框架的核心概念和实际应用,为Java编程中的数据存储与管理提供基础指导。
69 0
|
2月前
|
缓存 NoSQL Java
Java Web 从入门到精通之苍穹外卖项目实战技巧
本项目为JavaWeb综合实战案例——苍穹外卖系统,涵盖Spring Boot 3、Spring Cloud Alibaba、Vue 3等主流技术栈,涉及用户认证、订单处理、Redis缓存、分布式事务、系统监控及Docker部署等核心功能,助你掌握企业级项目开发全流程。
225 0
|
2月前
|
存储 安全 Java
Java 学习路线 35 掌握 List 集合从入门到精通的 List 集合核心知识
本文详细解析Java中List集合的原理、常用实现类(如ArrayList、LinkedList)、核心方法及遍历方式,并结合数据去重、排序等实际应用场景,帮助开发者掌握List在不同业务场景下的高效使用,提升Java编程能力。
267 0

热门文章

最新文章