同步容器并发容器

2017-08-18  本文已影响13人  zlcook

同步容器类

同步容器类的问题

迭代器与CncurrrentModificationException

隐藏的迭代器

并发容器

Queue、BlockingQueue容器接口

ConcurrentHashMap

CopyOnWriteArrayList、CopyOnWriteArraySet

阻塞队列和生产者-消费者模式

双端队列

阻塞方法与中断方法

 /** Lock held by take, poll, etc */
    private final ReentrantLock takeLock = new ReentrantLock();

    /** Wait queue for waiting takes */
    private final Condition notEmpty = takeLock.newCondition();

public E take() throws InterruptedException {
        E x;
        int c = -1;
        final AtomicInteger count = this.count;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lockInterruptibly();
        try {
            while (count.get() == 0) {
                notEmpty.await();
            }
            x = dequeue();
            c = count.getAndDecrement();
            if (c > 1)
                notEmpty.signal();
        } finally {
            takeLock.unlock();
        }
        if (c == capacity)
            signalNotFull();
        return x;
    }
上一篇 下一篇

猜你喜欢

热点阅读