美文网首页
双缓冲队列

双缓冲队列

作者: 良辰夜 | 来源:发表于2018-05-23 18:47 被阅读0次

百度上各种找,始终找不到,故只能自己写了

实现思想

java 的 LinkedBlockingQueue队列,会存在一个问题,即队列的入队和出队是互斥的,即某一刻有且仅有一个操作!
但是从逻辑上来说,出队和入队,不存在互斥的关系,那么是否可以不用实现相互不互斥呢?
基于这个想法,我们想到了双队列
即,一个队列专门入队,一个队列专门出队,当出对队列为空时,将两个队列切换,这样的话,就在一定程度上实现了,出对和入队不互斥。

代码


public class DoubleBlockingQueue<T> {

    private volatile boolean direction = true;

    private List<T> queue1 = new LinkedList<>();
    private List<T> queue2 = new LinkedList<>();

    private Object readLock = new Object();
    private Object writeLock = new Object();


    public void add(T o) {
        synchronized (readLock) {
            if (direction) {
                queue1.add(o);
            } else {
                queue2.add(o);
            }
        }
    }

    public T poll() {
        synchronized (writeLock) {
            if (direction) {
                return unqueue(queue2, queue1);
            }
            return unqueue(queue1, queue2);
        }
    }

    private T unqueue(List<T> poll, List<T> put) {
        if (poll.isEmpty()) {
            synchronized (readLock) {
                if (put.isEmpty())
                    return null;
                direction = !direction;
            }
            return put.remove(0);
        } else {
            return poll.remove(0);
        }
    }
}

测试效果

** 近提升了一倍的效率! **


public static void main(String[] args) throws InterruptedException {
        final int count = 10000 * 1000;

        test1(count);// 975
        test2(count);//1756

    }

    public static void test1(int count) throws InterruptedException {
        DoubleBlockingQueue<Integer> queue1 = new DoubleBlockingQueue<>();

        long l = System.currentTimeMillis();

        Thread thread = new Thread(() -> {
            int i = 0;
            while (i < count) {
                queue1.add(i++);
            }
        });
        thread.start();
        Thread thread1 = new Thread(() -> {
            while (true){
                Integer poll = queue1.poll();
                if(poll!=null &&  poll== count -1)
                    return ;
            }
        });
        thread1.start();

        thread.join();
        thread1.join();

        System.out.println(System.currentTimeMillis() - l);
    }

    public static void test2(int count) throws InterruptedException {
        LinkedBlockingQueue<Integer> queue1 = new LinkedBlockingQueue();

        long l = System.currentTimeMillis();

        Thread thread = new Thread(() -> {
            int i = 0;
            while (i < count) {
                queue1.add(i++);
            }
        });
        thread.start();
        Thread thread1 = new Thread(() -> {
            while (true){
                Integer poll = queue1.poll();
                if(poll!=null &&  poll== count -1)
                    return ;
            }
        });
        thread1.start();

        thread.join();
        thread1.join();

        System.out.println(System.currentTimeMillis() - l);
    }

相关文章

  • 双缓冲队列

    百度上各种找,始终找不到,故只能自己写了 实现思想 java 的 LinkedBlockingQueue队列,会存...

  • Ring buffer

    环形缓冲区也称为循环缓冲区。 基于数组的队列的问题是在队列后面添加新项目很快,O(1),但是从队列前面删除项目很慢...

  • 重构数据中心网络-新型传输协议、路由算法、拥塞算法

    CP(cut payload):在incast问题上,当前端服务器的缓冲队列占满时,后到的包和缓冲队列中的最后一个...

  • 7.双端队列Deque

    目录:1.双端队列的定义2.双端队列的图解3.双端队列定义操作4.双端队列的实现 1.双端队列的定义 2.双端队列...

  • 数据结构-队列(Queue)-FIFO

    数据结构-队列(Queue)-FIFO 队列的接口设计 双端队列-Deque 循环队列-CircleQueue 双...

  • 环形缓冲区

    下面引用维基百科,来学习环形缓冲区。 环形缓冲器 圆形缓冲区(circular buffer),也称作圆形队列(c...

  • 3、volley 官方教程-建立一个请求队列

    文章摘要1、volley 网络请求队列和缓冲请求队列2、volley 单例模式 英文文献 一、设定网络请求队列和缓...

  • 今日份打卡 176/365

    技术文章* 写缓冲* 一般写缓冲:以缓冲池为中心,双写缓冲池以及redo log* InnoDB优化写缓冲:以缓冲...

  • 线程池的阻塞队列

    1、线程池中阻塞队列的作用?一般的队列只能保证作为一个有限长度的缓冲区,如果超出了缓冲长度,就无法保留当前的任务了...

  • HDFS架构师 2.2 - NameNode元数据双缓冲写入原理

    (1)HDFS元数据管理、双缓冲 17 } 0:55 双缓冲写元数据的方法 ——》 FSEditLo...

网友评论

      本文标题:双缓冲队列

      本文链接:https://www.haomeiwen.com/subject/qehdjftx.html