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();
    }

  • 相关阅读:
    MySQL语法
    SQL必知必会
    大话设计模式
    软件工程
    myeclipse中git的使用
    提取文件中图片标签
    pandas 分析各地区男女的分布情况
    提取图片标签
    Requwsts+正则表达式爬取猫眼电影Top100
    PyCharm激活码
  • 原文地址:https://www.cnblogs.com/itxuexiwang/p/6270323.html
Copyright © 2011-2022 走看看