zoukankan      html  css  js  c++  java
  • Concurrent包学习之 BlockingQueue源码学习

    上一篇学习了ExecutorService和其它相关类的源码,本篇要学习的BlockingQueue中的源码,as always,先上类图

    其实继承(实现)的层次比较简单,我们只要需要先学习一下BlockingQueue中的方法:

    public interface BlockingQueue<E> extends Queue<E> {
        boolean add(E e);--往队列中插入一个对象,队列满了会抛异常
        boolean offer(E e);--同上,区别是队列满了会返回false
        void put(E e) throws InterruptedException;--插入一个元素,满了就等待
        boolean offer(E e, long timeout, TimeUnit unit)
            throws InterruptedException;--规定时间内插入元素
        E take() throws InterruptedException;--弹出队首
        E poll(long timeout, TimeUnit unit)
            throws InterruptedException;--规定时间内弹出队首
        int remainingCapacity();--队列剩余大小
        boolean remove(Object o);--删除一个equals o的对象
        public boolean contains(Object o);--是否包含o
        int drainTo(Collection<? super E> c);--把队列迁移到另外一个collection结构中
        int drainTo(Collection<? super E> c, int maxElements);--迁移,有个最大迁移数量
    }
    

    其实除了poll和offer 其它方法一般我们是用不到的,所以还是很简单的接口定义。下面去看一下具体的几个实现类。

    ArrayBlockingQueue--声明时就确定大小的队列,fifo方式。(方法基本和接口一致,没有特别要说明的内容)

    LinkedBlockingQueue--链表实现的queue-remove效率会高一些

    PriorityBlockingQueue--优先级队列

    SynchronousQueue--阻塞队列,必须拿走一个才能放进来一个,也就是最多只有一个~

    DelayQuque--就是放进去的内容,延迟时间到了后才可以获得

    --

    LinkedBlockDeque--双端队列 :offerFirst/offerLast,pollFirst/pollLast

    LinkedTransferQueue--类似LinkedUnBlockedQueue,其实就是transfer方法有人再等待队列内容就直接给他这个元素,没人在等就放在队列里面。也就是效率会更高。

  • 相关阅读:
    Centos 7.X 安装JDK1.8
    CentOS 7.X 搭建时间服务器 --- chrony
    CentOS 7.X 设置系统时间
    CentOS 7.X 关闭SELinux
    C11工具类:时间处理
    C11内存管理之道:智能指针
    C11性能之道:标准库优化
    C11性能之道:转移和转发
    C11性能之道:右值引用
    C11简洁之道:tupe元祖
  • 原文地址:https://www.cnblogs.com/congsg2016/p/5639105.html
Copyright © 2011-2022 走看看