zoukankan      html  css  js  c++  java
  • BlockingQueue 原理 分析

    ReentrantLock 两个两个比较大的特性。
    1.中断
    2.定时
    3.公平锁。

    ReadWriteLock

    读读不互斥
    读写互斥
    写写互斥。
    Condition 类似于 Object.wait()和Object.notify()和synchronized配套使用

    CountDownLatch lanchi
    synchronized

    static final CountDownLatch end = new CountDownLatch(10);
    end.countDown();
    end.await();

    CyclicBarrier 循环栅栏 CountDownLatch可以循环使用。

    Semaphore
    共享锁
    运行多个线程同时临界区

    ArrayBlockingQueue

    ReentrantLock
    +两个Condition

    /** Main lock guarding all access */
    final ReentrantLock lock;
    /** Condition for waiting takes */
    private final Condition notEmpty;
    /** Condition for waiting puts */
    private final Condition notFull;


    offer
    take
    当写入时。判断锁unlock 判断条件
    是否已经满了,如果慢了 等等 未满Condition的通知条件。

    调用insert 插入 然后notEmpty


    BlockingQueue


    public void put(E e) throws InterruptedException {
    checkNotNull(e);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
    while (count == items.length)
    notFull.await();
    insert(e);
    } finally {
    lock.unlock();
    }
    }


    private void insert(E x) {
    items[putIndex] = x;
    putIndex = inc(putIndex);
    ++count;
    notEmpty.signal();
    }

  • 相关阅读:
    BZOJ2253: [2010 Beijing wc]纸箱堆叠
    解题:CF1055F Tree and XOR
    解题:JSOI 2011 柠檬
    解题:NOI 2009 诗人小G
    2019.2.28&2019.3.1 考试
    省选前作业题汇总2
    解题:LNOI 2014 LCA
    省选前作业题汇总1
    2019.2.26 考试
    解题:SDOI 2014 重建
  • 原文地址:https://www.cnblogs.com/itxuexiwang/p/6270323.html
Copyright © 2011-2022 走看看