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