文章目录
本文整个体系和内容参考了《Java并发编程的艺术》和JDK8的JUC源码,JUC源码的注解写的还是很详细的。
Lock接口
锁是用来控制多个线程访问共享资源的方式,一般来说,一个锁能够防止多个线程同时访问共享资源(但是有些锁可以允许多个线程并发的访问共享资源,比如读写锁)。在Lock接口出现之前,Java程序是靠synchronized关键字实现锁功能的,而Java 5之后,并发包中新增了Lock接口(以及相关实现类)用来实现锁功能,它提供了与synchronized关键字类似的同步功能,只是在使用时需要显式地获取和释放锁。虽然它缺少了(通过synchronized块或者方法所提供的)隐式获取释放锁的便捷性,但是却拥有了锁获取与释放的可操作性、可中断的获取锁以及超时获取锁等多种synchronized关键字所不具备的同步特性。
class X{
//定义锁对象
private final ReentrantLock lock=new ReentrantLock();
//定义需要保证线程安全的方法
public void m(){
//加锁
lock.lock();
try{
//...method body
}
//使用finally块来保证释放锁
finally{
lock.unlock();
}
}
}
使用Reentrantlock可以进行尝试锁定**tryLock()
**,这样无法锁定,或者在指定时间内无法锁定,返回false;
使用ReentrantLock还可以调用**lockInterruptibly()
**方法,可以对线程interrupt()方法做出响应,在一个线程等待锁的过程中,可以被打断,打断后会抛异常。
自己实现一个锁
方法一:通过自旋实现一个锁
public class SpinLock {
//原子引用线程
AtomicReference<Thread> atomicReference = new AtomicReference<>();
public void mylock() {
Thread thread = Thread.currentThread();
System.out.println(Thread.currentThread().getName() + "\t come in");
// 自旋获取锁
while (!atomicReference.compareAndSet(null, thread)) {
}
}
public void myUnlock() {
Thread thread = Thread.currentThread();
// CAS解锁
atomicReference.compareAndSet(thread, null);
System.out.println(Thread.currentThread().getName()+"\t invoked myunlock()");
}
}
缺点:耗费CPU资源,没有竞争到锁的线程会一直占用CPU资源进行CAS操作。
方法二:改进为park+自旋实现锁:
Java提供了一个较为底层的并发工具类:LockSupport,可以让线程停止下来(阻塞),还可以唤醒线程。
// 阻塞线程
LockSupport.park(Object blocker)
// 唤醒线程
LockSupport.unpark(Thread thread)
public class SpinLock {
// 原子引用线程
AtomicReference<Thread> atomicReference = new AtomicReference<>();
// 阻塞线程队列
Queue<Thread> parkQueue = new LinkedBlockingQueue<>();
public void mylock() {
System.out.println(Thread.currentThread().getName() + "\t come in");
// 自旋获取锁
while (!atomicReference.compareAndSet(null, thread)) {
park();
}
}
public void myUnlock() {
Thread thread = Thread.currentThread();
// CAS解锁
atomicReference.compareAndSet(thread, null);
System.out.println(Thread.currentThread().getName()+"\t invoked myunlock()");
lock_notify();
}
public void park() {
parkQueue.add(Thread.currentThread());
LockSupport.park(Thread.currentThread());
}
public void unpark() {
Thread t = parkQueue.poll();
LockSupport.unpark(t);
}
}
队列同步器AQS
队列同步器AbstractQueuedSynchronizer(AQS)是用来构建锁或者其他同步组件的基础框架,它使用了一个int成员变量表示同步状态,通过内置的FIFO队列来完成资源获取线程的排队工作,并发包的作者(Doug Lea)期望它能够成为实现大部分同步需求的基础。
AQS的实现
FIFO队列
同步器依赖内部的同步队列(一个FIFO双向队列)来完成同步状态的管理,当前线程获取同步状态失败时,同步器会将当前线程以及等待状态等信息构造成为一个节点(Node)并将其加入同步队列,同时会阻塞当前线程,当同步状态释放时,会把首节点中的线程唤醒,使其再次尝试获取同步状态。
AQS中的节点Node:
static final class Node {
// 等待状态,若值为-1,表示后继节点处于等待状态
volatile int waitStatus;
// 前一个节点
volatile Node prev;
// 下一个节点
volatile Node next;
// 节点绑定线程
volatile Thread thread;
}
AQS的属性:
public abstract class AbstractQueuedSynchronizer {
// 等待队列头结点
private transient volatile Node head;
// 等待队列尾结点
private transient volatile Node tail;
// 状态
private volatile int state;
}
独占锁同步状态的获取
调用aquire(1)方法:
public final void acquire(int arg) {
// 尝试获取同步器tryAcquire false--> 入队addWaiter --> park阻塞该线程acquireQueued
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
公平锁的tryAcquire(子类重写):
protected final boolean tryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();
if (c == 0) {
// 如果没有后继节点,把stata CAS置1,把exclusiveOwnerThread置为当前线程
if (!hasQueuedPredecessors() &&
compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
// 可重入锁
else if (current == getExclusiveOwnerThread()) {
int nextc = c + acquires;
if (nextc < 0)
throw new Error("Maximum lock count exceeded");
// 重入时 state + 1
setState(nextc);
return true;
}
return false;
}
判断是否有后续节点:
public final boolean hasQueuedPredecessors() {
Node t = tail;
Node h = head;
Node s;
return h != t &&
((s = h.next) == null || s.thread != Thread.currentThread());
}
实例化一个Node:
private Node addWaiter(Node mode) {
Node node = new Node(Thread.currentThread(), mode);
// 维护一个链表,prev和next节点
Node pred = tail;
if (pred != null) {
// 把Node加入尾部
node.prev = pred;
if (compareAndSetTail(pred, node)) {
pred.next = node;
return node;
}
}
enq(node);
return node;
}
// 把Node加入队列,必要时初始化
private Node enq(final Node node) {
// 自旋CAS
// 当有大量的线程在同时入队的时候,同一时刻,只有一个线程能完整地完成这三步,而其他线程只能完成第一步,于是就出现了尾分叉.
// 所有节点都会通过自旋不断的尝试入队,直到成功为止。
for (;;) {
Node t = tail;
if (t == null) { // Must initialize
// 实例化一个Thread为null的Node,并赋值给AQS的头
if (compareAndSetHead(new Node()))
tail = head;
} else {
// 把当前线程Node入队
node.prev = t;
if (compareAndSetTail(t, node)) {
t.next = node;
return t;
}
}
}
}
注:head指向的Node中的Thread永远为空,持有锁的线程不再在队列中。
acquireQueued
该方法是AQS的核心,addWaiter()
将当前线程加入队列后,先自旋2次,使用acquireQueued()
进行阻塞,中间可能被唤醒,但直到获取到资源后才返回,否则继续被park阻塞。
final boolean acquireQueued(final Node node, int arg) {
boolean failed = true;
try {
boolean interrupted = false;
for (;;) {
final Node p = node.predecessor();
// 判断是否为第一个元素(队列中第二个Node)
// 如果是第一个元素,自旋去获取锁,把当前节点置为head,把原head分离队列,方便GC
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // help GC
failed = false;
return interrupted;
}
//阻塞当前线程park,获取不到的话自旋2次
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
interrupted = true;
}
} finally {
if (failed)
cancelAcquire(node);
}
}
// 把当前节点置为head,Thread置为null
private void setHead(Node node) {
head = node;
node.thread = null;
node.prev = null;
}
判断是否需要阻塞:
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
int ws = pred.waitStatus;
if (ws == Node.SIGNAL)
// 前一个线程节点的waitState=-1,可以阻塞了
return true;
if (ws > 0) {
/*
* Predecessor was cancelled. Skip over predecessors and
* indicate retry.
*/
do {
node.prev = pred = pred.prev;
} while (pred.waitStatus > 0);
pred.next = node;
} else {
// 将前一个节点waitState设置为-1,目的是多一次自旋
compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
}
return false;
}
阻塞当前线程:
private final boolean parkAndCheckInterrupt() {
LockSupport.park(this);
return Thread.interrupted();
}
独占锁同步状态的释放
释放锁release(1):
public void unlock() {
sync.release(1);
}
public final boolean release(int arg) {
if (tryRelease(arg)) {
Node h = head;
if (h != null && h.waitStatus != 0)
//唤醒第一个排队Node
unparkSuccessor(h);
return true;
}
return false;
}
protected final boolean tryRelease(int releases) {
int c = getState() - releases;
if (Thread.currentThread() != getExclusiveOwnerThread())
throw new IllegalMonitorStateException();
boolean free = false;
// state = 0时把OwnerThread设为null
if (c == 0) {
free = true;
setExclusiveOwnerThread(null);
}
setState(c);
return free;
}
唤醒下一个节点:
private void unparkSuccessor(Node node) {
int ws = node.waitStatus;
if (ws < 0)
compareAndSetWaitStatus(node, ws, 0);
Node s = node.next;
if (s == null || s.waitStatus > 0) {
s = null;
// 从后向前找
// 因为一个节点要能入队,则它的prev属性一定是有值的,但是它的next属性可能暂时还没有值。
for (Node t = tail; t != null && t != node; t = t.prev)
if (t.waitStatus <= 0)
s = t;
}
if (s != null)
LockSupport.unpark(s.thread);
}
流程图
AQS的使用方法
同步器的主要使用方式是继承,子类通过继承同步器并实现它的抽象方法来管理同步状态,在抽象方法的实现过程中免不了要对同步状态进行更改,这时就需要使用同步器提供的3个方法getState()
、setState(int newState)
和compareAndSetState(int expect,int update))
来进行操作,因为它们能够保证状态的改变是安全的。子类推荐被定义为自定义同步组件的静态内部类,同步器自身没有实现任何同步接口,它仅仅是定义了若干同步状态获取和释放的方法来供自定义同步组件使用,同步器既可以支持独占式地获取同步状态,也可以支持共享式地获取同步状态,这样就可以方便实现不同类型的同步组件(ReentrantLock
、ReentrantReadWriteLock
和CountDownLatch
等)。
例如在ReentrantLock中,Sync为继承于AQS的静态内部类:
abstract static class Sync extends AbstractQueuedSynchronizer{}
AQS的接口
同步器的设计是基于模板方法模式的,也就是说,使用者需要继承同步器并重写指定的方法,随后将同步器组合在自定义同步组件的实现中,并调用同步器提供的模板方法,而这些模板方法将会调用使用者重写的方法。
同步器有三种核心方法,同步器中使用int变量表示同步状态,重写同步器的指定方法,需要调用这三种核心方法来访问或更新同步状态:
int getState()
:获取同步状态的当前值void setState(int newState)
:设置同步状态的值boolean compareAndSetState(int expect, int update)
:使用CAS原子性地设置同步状态的值,当同步状态的当前值为expect时,将其设置为给定值(update)。
AQS同步器可重写的方法包括独占式的获取或释放同步状态、共享式的获取或释放同步状态、同步器是否在独占模式下被线程占用,重写这几个方法是实现一个锁的核心。
boolean tryAcquire(int arg)
:独占式的获取同步状态,实现该方法需要先获取并判断同步状态是否符合预期,再通过CAS设置同步状态。(即使用compareAndSetState(expect, update)方法)。可用于实现Lock接口中的tryLock()方法。boolean tryRelease(int arg)
:独占式的释放同步状态,等待获取同步状态的线程将有机会获取同步状态;int tryAcquireShared(int arg)
:共享式的获取同步状态,返回大于等于0表示获取成功,否则获取失败;boolean tryReleaseShared(int arg)
: 共享式的释放同步状态;boolean isHeldExclusively()
: 用于获取同步器是否在独占模式下被线程占用,一般用来表示是否被当前线程所独占。
AQS提供的模板方法供锁来调用:
共享式同步状态获取与释放
以自定义同步组件——TwinsLock展示如何实现共享式锁:
public class TwinsLock implements Lock {
private final Sync sync = new Sync(2);
private static final class Sync extends AbstractQueuedSynchronizer {
Sync(int count) {
if (count <= 0) {
throw new IllegalArgumentException("count must large than zero.");
}
setState(count);
}
public int tryAcquireShared(int reduceCount) {
for (;;) {
int current = getState();
int newCount = current - reduceCount;
if (newCount < 0 || compareAndSetState(current,newCount)) {
return newCount;
}
}
}
public boolean tryReleaseShared(int returnCount) {
for (;;) {
int current = getState();
int newCount = current + returnCount;
if (compareAndSetState(current, newCount)) {
return true;
}
}
}
}
public void lock() {
sync.acquireShared(1);
}
public void unlock() {
sync.releaseShared(1);
}
// 其他接口方法略
}
核心:调用AQS中的模板方法acquireShared()
与releaseShared()
,重写了tryAcquireShared
与tryReleaseShared
方法。
独占式超时获取同步状态
通过调用同步器的doAcquireNanos(int arg, long nanosTimeout)
方法可以超时获取同步状态,即在指定的时间段内获取同步状态,如果获取到同步状态则返回true,否则,返回false。
private boolean doAcquireNanos(int arg, long nanosTimeout)
throws InterruptedException {
if (nanosTimeout <= 0L)
return false;
final long deadline = System.nanoTime() + nanosTimeout;
final Node node = addWaiter(Node.EXCLUSIVE);
boolean failed = true;
try {
for (;;) {
final Node p = node.predecessor();
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // help GC
failed = false;
return true;
}
// 计算还能等待多长时间
nanosTimeout = deadline - System.nanoTime();
if (nanosTimeout <= 0L)
return false;
if (shouldParkAfterFailedAcquire(p, node) &&
nanosTimeout > spinForTimeoutThreshold)
LockSupport.parkNanos(this, nanosTimeout);//最多让当前线程park这么久
if (Thread.interrupted())
throw new InterruptedException();
}
} finally {
if (failed)
cancelAcquire(node);
}
}
park()会让当前线程进入waiting状态。在此状态下,有两种途径可以唤醒该线程:
- 被unpark();
- 被interrupt()
可打断的获取锁
如果本线程是处于获取锁状态:调用线程的wait()
,wait(long)
或wait(long, int)
会让它进入等待(阻塞)状态,或者调用线程的join()
, join(long)
, join(long, int)
, sleep(long)
, sleep(long, int)
也会让它进入阻塞状态。若线程在阻塞状态时,调用了它的interrupt()
方法,那么**它的中断状态会被清除并且会收到一个InterruptedException
异常。**需要注意的是,Thread.interrupted()
会清除当前线程的中断标记位。
注意:synchronized和lock()在等待锁的时候无法被打断,而lockInterruptibly()可以被打断,抛出异常。
lockInterruptibly()的实现方法为:
public void lockInterruptibly() throws InterruptedException {
sync.acquireInterruptibly(1);
}
public final void acquireInterruptibly(int arg) throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
if (!tryAcquire(arg))
doAcquireInterruptibly(arg);
}
private void doAcquireInterruptibly(int arg)
throws InterruptedException {
final Node node = addWaiter(Node.EXCLUSIVE);
boolean failed = true;
try {
for (;;) {
final Node p = node.predecessor();
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // help GC
failed = false;
return;
}
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
// 当park的时候被interrupt时,抛出异常
throw new InterruptedException();
}
} finally {
if (failed)
cancelAcquire(node);
}
}
可以与tryAcquire()对比。
重入锁ReentrantLock
重入锁ReentrantLock,顾名思义,就是支持重进入的锁,它表示该锁能够支持一个线程对资源的重复加锁。
可重入锁(也叫作递归锁),指的时同一线程外层函数获得锁之后,内层递归函数仍然能获取该锁的代码,在同一个线程在外层方法获取锁的时候,在进入内层方法会自动获取锁,也就是说,线程可以进入任何一个它已经拥有的锁所同步着的代码块。可重入锁最大的作用是避免死锁。
protected final boolean tryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();
if (c == 0) {
// 如果没有后继节点,把stata CAS置1,把exclusiveOwnerThread置为当前线程
if (!hasQueuedPredecessors() &&
compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
// 可重入锁
else if (current == getExclusiveOwnerThread()) {
int nextc = c + acquires;
if (nextc < 0)
throw new Error("Maximum lock count exceeded");
// 重入时 state + 1
setState(nextc);
return true;
}
return false;
}
可重入锁主要解决了两个问题:
- 线程再次获取锁。锁需要去识别获取锁的线程是否为当前占据锁的线程,如果是,则再次成功获取。
- 锁的最终释放。线程重复n次获取了锁,随后在第n次释放该锁后,其他线程能够获取到该锁。锁的最终释放要求锁对于获取进行计数自增,计数表示当前锁被重复获取的次数,而锁被释放时,计数自减,当计数等于0时表示锁已经成功释放。
公平与非公平锁
ReentrantLock还可以实现非公平锁,其区别在于非公平锁在加锁时先进行了一次CAS获取锁的尝试,如果获取到锁,直接执行,不需要排队阻塞。
非公平锁:
final void lock() {
if (compareAndSetState(0, 1))
setExclusiveOwnerThread(Thread.currentThread());
else
acquire(1);
}
公平锁:
final void lock() {
acquire(1);
}
刚释放锁的线程再次获取同步状态的几率会非常大,使得其他线程只能在同步队列中等待。
公平性锁保证了锁的获取按照FIFO原则,而代价是进行大量的线程切换。非公平性锁虽然可能造成线程“饥饿”,但极少的线程切换,保证了其更大的吞吐量。
读写锁
写锁是独占的,当写锁被获取到时,后续(非当前写操作线程)的读写操作都会被阻塞,写锁释放之后,所有操作继续执行。
读锁是共享的,多个线程可以共同读取数据。
一般情况下,读写锁的性能都会比排它锁好,因为大多数场景读是多于写的。在读多于写的情况下,读写锁能够提供比排它锁更好的并发性和吞吐量。Java并发包提供读写锁的实现是ReentrantReadWriteLock。
读写锁使用案例:
public class Cache {
static Map<String, Object> map = new HashMap<String, Object>();
static ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
static Lock r = rwl.readLock();
static Lock w = rwl.writeLock();
// 获取一个key对应的value
public static final Object get(String key) {
r.lock();
try {
return map.get(key);
} finally {
r.unlock();
}
}
// 设置key对应的value,并返回旧的value
public static final Object put(String key, Object value) {
w.lock();
try {
return map.put(key, value);
} finally {
w.unlock();
}
}
// 清空所有的内容
public static final void clear() {
w.lock();
try {
map.clear();
} finally {
w.unlock();
}
}
}
读写锁的实现原理:
读写锁同样依赖自定义同步器来实现同步功能,而读写状态就是其同步器的同步状态。读写锁的自定义同步器需要在同步状态(一个整型变量)上维护多个读线程和一个写线程的状态。
如果在一个整型变量上维护多种状态,就一定需要按位切割使用这个变量,读写锁将变量切分成了两个部分,高16位表示读,低16位表示写,划分方式如图(当前同步状态表示一个线程已经获取了写锁,且重进入了两次,同时也连续获取了两次读锁):
写锁是一个支持重进入的排它锁。如果当前线程已经获取了写锁,则增加写状态。如果当前线程在获取写锁时,读锁已经被获取(读状态不为0)或者该线程不是已经获取写锁的线程,则当前线程进入等待状态。
protected final boolean tryAcquire(int acquires) {
/*
* Walkthrough:
* 1. If read count nonzero or write count nonzero
* and owner is a different thread, fail.
* 2. If count would saturate, fail. (This can only
* happen if count is already nonzero.)
* 3. Otherwise, this thread is eligible for lock if
* it is either a reentrant acquire or
* queue policy allows it. If so, update state
* and set owner.
*/
Thread current = Thread.currentThread();
int c = getState();
// 获取写锁状态值
int w = exclusiveCount(c);
if (c != 0) {
// (Note: if c != 0 and w == 0 then shared count != 0)
if (w == 0 || current != getExclusiveOwnerThread())
return false;
if (w + exclusiveCount(acquires) > MAX_COUNT)
throw new Error("Maximum lock count exceeded");
// Reentrant acquire
setState(c + acquires);
return true;
}
if (writerShouldBlock() ||
!compareAndSetState(c, c + acquires))
return false;
setExclusiveOwnerThread(current);
return true;
}
读锁是一个支持重进入的共享锁,它能够被多个线程同时获取,在没有其他写线程访问(或者写状态为0)时,读锁总会被成功地获取,而所做的也只是(线程安全的)增加读状态。
protected final int tryAcquireShared(int unused) {
/*
* Walkthrough:
* 1. If write lock held by another thread, fail.
* 2. Otherwise, this thread is eligible for
* lock wrt state, so ask if it should block
* because of queue policy. If not, try
* to grant by CASing state and updating count.
* Note that step does not check for reentrant
* acquires, which is postponed to full version
* to avoid having to check hold count in
* the more typical non-reentrant case.
* 3. If step 2 fails either because thread
* apparently not eligible or CAS fails or count
* saturated, chain to version with full retry loop.
*/
Thread current = Thread.currentThread();
int c = getState();
if (exclusiveCount(c) != 0 &&
getExclusiveOwnerThread() != current)
return -1;
int r = sharedCount(c);
if (!readerShouldBlock() &&
r < MAX_COUNT &&
compareAndSetState(c, c + SHARED_UNIT)) {
if (r == 0) {
firstReader = current;
firstReaderHoldCount = 1;
} else if (firstReader == current) {
firstReaderHoldCount++;
} else {
HoldCounter rh = cachedHoldCounter;
if (rh == null || rh.tid != getThreadId(current))
cachedHoldCounter = rh = readHolds.get();
else if (rh.count == 0)
readHolds.set(rh);
rh.count++;
}
return 1;
}
return fullTryAcquireShared(current);
}
Condition接口
任意一个Java对象,都拥有一组监视器方法(定义在java.lang.Object
上),主要包括wait()
、wait(long timeout)
、notify()
以及notifyAll()
方法,这些方法与synchronized同步关键字配合,可以实现等待/通知模式。Condition接口也提供了类似Object的监视器方法,与Lock配合可以实现等待/通知模式。
使用方法:
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
public void conditionWait() throws InterruptedException {
lock.lock();
try {
condition.await();
} finally {
lock.unlock();
}
}
public void conditionSignal() throws InterruptedException {
lock.lock();
try {
condition.signal();
// condition.signalAll();
} finally {
lock.unlock();
}
}
实现原理:
ConditionObject
是同步器AbstractQueuedSynchronizer
的内部类,因为Condition
的操作需要获取相关联的锁,所以作为同步器的内部类也较为合理。每个Condition
对象都包含着一个队列(以下称为等待队列),该队列是Condition对象实现等待/通知功能的关键。
等待队列:
等待队列是一个FIFO的队列,在队列中的每个节点都包含了一个线程引用,该线程就是在Condition对象上等待的线程,如果一个线程调用了Condition.await()
方法,那么该线程将会释放锁、构造成节点加入等待队列并进入等待状态。事实上,节点的定义复用了同步器中节点的定义,也就是说,同步队列和等待队列中节点类型都是同步器的静态内部类AbstractQueuedSynchronizer.Node
。
在Object的监视器上,一个对象拥有一个同步队列和等待队列,而同步器拥有一个同步队列和多个等待队列。
等待await:
当前线程调用await()方法时,会使当前线程进入等待队列并释放锁,同时线程状态变为等待状态,相当于同步队列的首节点(获取了锁的节点)移动到Condition的等待队列中。
ConditionObject的await()方法:
public final void await() throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
// 当前线程加入等待队列
Node node = addConditionWaiter();
// 释放同步状态,也就是释放锁
int savedState = fullyRelease(node);
int interruptMode = 0;
while (!isOnSyncQueue(node)) {
LockSupport.park(this);
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
}
if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
interruptMode = REINTERRUPT;
if (node.nextWaiter != null)
unlinkCancelledWaiters();
if (interruptMode != 0)
reportInterruptAfterWait(interruptMode);
}
调用该方法的线程成功获取了锁的线程,也就是同步队列中的首节点,该方法会将当前线程构造成节点并加入等待队列中,然后释放同步状态,唤醒同步队列中的后继节点,然后当前线程会进入等待状态。
通知signal():
调用Condition的signal()方法,将会唤醒在等待队列中等待时间最长的节点(首节点),在唤醒节点之前,会将节点移到同步队列中。
public final void signal() {
if (!isHeldExclusively())
throw new IllegalMonitorStateException();
Node first = firstWaiter;
if (first != null)
doSignal(first);
}
通过调用同步器的enq(Node node)
方法,等待队列中的头节点线程安全地移动到同步队列。当节点移动到同步队列后,当前线程再使用LockSupport唤醒该节点的线程。被唤醒后的线程,将从await()方法中的while循环中退出(isOnSyncQueue(Node node)
方法返回true,节点已经在同步队列中),进而调用同步器的acquireQueued()
方法加入到获取同步状态的竞争中。成功获取同步状态之后,被唤醒的线程将从先前调用的await()方法返回,此时该线程已经成功地获取了锁。
Condition的signalAll()方法,相当于对等待队列中的每个节点均执行一次signal()方法,效果就是将等待队列中所有节点全部移动到同步队列中,并唤醒每个节点的线程。