栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

BlockingQueue的实现类讲解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

BlockingQueue的实现类讲解

jdk有几个BlockingQueue阻塞队列的实现类,总结一下,不同点及用处。

测试代码

package com.example.demo2.cusdemo.queuedemo;

import java.util.Random;
import java.util.concurrent.*;


public class TestBlockQueue {
    public static void main(String[] args) {
//        BlockingQueue q = new ArrayBlockingQueue(3);
//        BlockingQueue q = new PriorityBlockingQueue(3);
//        BlockingQueue q = new SynchronousQueue();
        BlockingQueue q = new DelayQueue();
//        BlockingQueue q = new linkedBlockingQueue(3);
        Producer p = new Producer(q);
        Consumer c1 = new Consumer(q);
        Consumer c2 = new Consumer(q);
        new Thread(p).start();
        new Thread(c1).start();
        new Thread(c2).start();
    }

}

class Producer implements Runnable {
    private final BlockingQueue queue;
    Producer(BlockingQueue q) { queue = q; }
    public void run() {
        try {
            while (true) { queue.put(produce()); }
//            while (true) { queue.add(produce()); }
//            while (true) { queue.offer(produce(),5, TimeUnit.MILLISECONDS); }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    Object produce() {
        return new Random().nextInt(50);
    }
}

class Consumer implements Runnable {
    private final BlockingQueue queue;
    Consumer(BlockingQueue q) { queue = q; }
    public void run() {
        try {
            while (true) { consume(queue.take()); }
        } catch (InterruptedException ex) {

        }
    }
    void consume(Object x) {
        System.out.println(Thread.currentThread().getName()+"---消费者---"+x);
    }
}

看类图

第一个:ArrayBlockingQueue

入队方法

public boolean add(E e) ---- 内部会调用offer(e),返回false就抛异常,需要自己捕获,否则线程遇到这个异常就结束了

public boolean offer(E e)----往队列里添加元素,添加成功返回true,添加失败返回false。
public void put(E e) throws InterruptedException---这个方法,使用最多,但是要注意使用场景,往队列添加元素时,队列满了,线程会进入阻塞,等元素消费为0时,再唤醒线程添加。在实际使用中,就会造成超时。任务添加失败。需要使用者综合考虑
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException---

这个方法是,如果队列满了,就阻塞一段时间,再判断队列中是否有空位置,有---再进行入队,入队成功返回true  ;没有count == items.length,就返回false;

出队方法
public E poll()----队列中count=0就返回null,否则返回队列中的元素
public E take() throws InterruptedException----从队列中取值,队列中有元素,就返回,没有就阻塞,和put()方法配合使用
public E poll(long timeout, TimeUnit unit) throws InterruptedException---队列有元素,取出返回,没有就阻塞一段时间,看队列中是否有元素,还没有返回null。
public E peek(),从队列中取出第一个元素,有可能为null
优点

数组队列,采用的是环形数组,存取效率比较高。但是存取用的是同一个锁lock。这也体现了读写互斥原则。效率相对就慢了。

第二个:linkedBlockingQueue

存取采用了2个锁

private final ReentrantLock takeLock = new ReentrantLock();

    
    private final Condition notEmpty = takeLock.newCondition();

    
    private final ReentrantLock putLock = new ReentrantLock();

    
    private final Condition notFull = putLock.newCondition();

 这是一个单项链表结构

static class Node {
        E item;

        
        Node next;

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

这个存取效率相对比较高

第三个:PriorityBlockingQueue

 优先级队列:这个队列维护了一个小顶堆,会根据排序规则,堆顶存最小的元素。如果排序规则变化,就是大顶堆了。每次取出的时候,都是取顶部元素。

使用场景:会员优先买票。级别高的优先做某些事情。

第四个:SynchronousQueue

同步队列,可以指定公平和非公平方式。队列中每次放一个任务,放第二个的时候就阻塞了,等待消费者,消费完,才会唤醒生成线程,继续王队列放任务。

第五个:DelayQueue

延时队列,这个会计算延时多久,然后线程将阻塞一段时间,再继续执行任务。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/733105.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号