1. java中锁的分类
java API层面的ReentrantLock,JVM层面的Synchronized
2. 多线程锁升级原理
- 无锁
没有对资源进行锁定,所有的线程都能够访问并修改资源,但最终只有一个线程可以执行成功 - 偏向锁
某一段代码一直被一个线程执行,不存在多个线程竞争,该线程在后续执行过程中自动获取锁 - 轻量级锁
当锁是偏向锁的时候,被第二个线程B访问,线程B会通过自旋的方式获取锁,锁就升级为轻量级锁 - 重量级锁
当一个线程获取锁以后,其他等待获取锁的线程处于阻塞状态
3. ReentrantLock理解
-
类图
-
加锁过程,以公平锁为例
-
创建锁,指定锁的公平性
Lock lock = new ReentrantLock(true);/** * Creates an instance of {@code ReentrantLock} with the * given fairness policy. * * @param fair {@code true} if this lock should use a fair ordering policy */ public ReentrantLock(boolean fair) { sync = fair ? new FairSync() : new NonfairSync(); }
-
调用FairSync类lock()获取锁
final void lock() {
acquire(1);
} -
调用AbstractQueuedSynchronizer类acquire()获取锁
public final void acquire(int arg) {
// 如果加锁成功,不需要进入等待队列,反之进入等待队列
if (!tryAcquire(arg) &&
// 把当前线程挂起,放入阻塞队列中
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
// 线程中断
selfInterrupt();
} -
FairSync类tryAcquire()源码分析
-
protected final boolean tryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();
// state=0,此时没有线程持有锁
if (c == 0) {
// 如果没有线程在队列中等待时间比当前线程长,使用CAS尝试获取锁,成功了就获取到锁了,反之其他线程获取
if (!hasQueuedPredecessors() &&
compareAndSetState(0, acquires)) {
// 获取到锁,设置锁为当前线程拥有者
setExclusiveOwnerThread(current);
return true;
}
}
// c > 0表示当前线程已经获取到锁了,再次进入这个方法只能是重进入了,
需要更新锁的状态。如果当前线程是锁的拥有者,冲进入
else if (current == getExclusiveOwnerThread()) {
int nextc = c + acquires;
if (nextc < 0)
// 重进入次数太多,超过int最大值,变成负数
throw new Error("Maximum lock count exceeded");
setState(nextc);
return true;
}
return false;
}
5.AbstractQueuedSynchronized类addWaiter()
// 将线程包装成Node,进入队列
private Node addWaiter(Node mode) {
Node node = new Node(Thread.currentThread(), mode);
// 把Node加到链表最后面去,所有Node通过前驱节点、队尾节点形成双向无环链表
Node pred = tail;
if (pred != null) {
node.prev = pred;
if (compareAndSetTail(pred, node)) {
pred.next = node;
return node;
}
}
// 如果队列是空的,创建队列,重新入队
enq(node);
return node;
}
6.AbstractQueuedSynchronized类enq()
private Node enq(final Node node) {
// 采用自旋方式入队
for (;;) {
// 队尾节点
Node t = tail;
if (t == null) { // Must initialize
// 初始化head节点,初始化队列
if (compareAndSetHead(new Node()))
// 这时候head有了,tail还是null,设置一下
tail = head;
} else {
// 将当前线程接入队尾,维护双向链表关系
node.prev = t;
if (compareAndSetTail(t, node)) {
t.next = node;
return t;
}
}
}
}
7.AbstractQueuedSynchronized类acquireQueued()
// 参数Node经过addWaiter已经进入阻塞队列
final boolean acquireQueued(final Node node, int arg) {
boolean failed = true;
try {
boolean interrupted = false;
// 无限循环
for (;;) {
final Node p = node.predecessor();
// p=head,说明当前节点进入了阻塞队列并且是队列第一个,head一般指占有锁的线程
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // help GC
failed = false;
return interrupted;
}
// 没有抢到锁,是否需要挂起当前线程
if (shouldParkAfterFailedAcquire(p, node) &&
// 当前线程被挂起,等待唤醒
parkAndCheckInterrupt())
interrupted = true;
}
} finally {
if (failed)
// 取消排队
cancelAcquire(node);
}
}
7.AbstractQueuedSynchronized类shouldParkAfterFailedAcquire()
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
// 如果前驱节点waitStatus=-1,说明前驱节点状态正常,让前驱节点唤醒当前节点
int ws = pred.waitStatus;
if (ws == Node.SIGNAL)
/*
* This node has already set status asking a release
* to signal it, so it can safely park.
*/
return true;
// 如果前驱节点waitStatus>0,说明前驱节点取消了排队,需要找到前驱节点的前驱节点唤醒当前线程
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 {
/*
* waitStatus must be 0 or PROPAGATE. Indicate that we
* need a signal, but don't park yet. Caller will need to
* retry to make sure it cannot acquire before parking.
*/
//用CAS将前驱节点waitStatus设置为-1
compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
}
return false;
}
4. Synchronized理解