说明 CAS操作
比较并交换
CAS3个操作数,内存值V,旧的预期值A,要修改的更新值B。当且仅当预期值A与内存值V相同时,将内存值V修改为B,否则什么都不做。
重入锁 ReentrantLock
public class ReentrantLock implements Lock,Serializabke{
private final ReentrantLock.Sync sync
//无参构造函数 使用的非公平的锁
public ReentrantLock(){
this.sync = new ReentrantLock.NonFairSync();
}
//true 公平锁(先进来的线程先执行),否则非公平锁(后近来的线程也可能先执行)
public ReentranLock(boolean var1){
this.sync = (ReentrantLock.Sync)(var1 ? new ReentrantLock.FairSync : new ReentrantLock.NonfairSync);
}
//获得锁
public void lock(){
this.sync.lock();
}
//获得锁,优先响应中断,无法获取锁会被中断
public void lockInterruptibly() throws InterruptedException{
this.sync.acquireInterruptiby(1);
}
//尝试获得锁,该方法不等待,成功返回true 失败返回false
public boolean tryLock(){
return this.sync.nonfairTryAcquire(1);
}
//在给定的时间内尝试获得锁
public boolean tryLock(long var1 , TimeUnit var2) throws InterruptedException{
return this.sync.tryAcquireNanos(1,var2.toBanos(v1));
}
//释放锁
public void unlock(){
this.sync.release(1);
}
public Condition newCondition(){
return this.sync.newCondition();
}
static final class FairSync extends ReentrantLock.Sync{}
static final class NonfairSync extends ReentrantLock.Sync{
//尝试获取锁
final void lock(){
//先基于cas将state(锁数量)重0设置1, 成功,设置当前线程为独占线程;请求成功则插队
//失败,即尝试的过程中,被其它线程先一步占有锁
if([this.compareAndSetState(0](http://this.compareandsetstate(0/),1)){
this.setExclusiveOwnerThread(Thread.currentThread());
}else{
// 1调用acquire(1) 会先调用tryAcquire, 再次尝试获取锁,和当前线程的是否是独占锁线程的判断
// tryAcquire 返回false ,则执行this.acquireQueued(this.addWaiter(AbstractQueuedSynchronizer.Node.EXCLUSIVE), var1)
this.acquire(1);
}
}
protected final boolean tryAcquire(int var1){
return this.nonfairTryAcquire(var1);
}
}
abstract static class Sync extends AbstractQueuedSynchronizer{}
final boolean nonfairTryAcquire(int var1){
Thread var2 = Thread.currentThread();
//首先获取状态锁的数量, 是0,表示当前锁已经被释放,
int var3 = this.getState();
if(var3 == 0){
//继续基于cas将state(锁数量)重0设置1,成功,设置当前线程为独占线程;请求成功则插队;
if([this.compareAndSetState(0](http://this.compareandsetstate(0/),1)){
this.setExclusiveOwnerThread(Thread.currentThread());
return true;
}
}else if(var2 == this.getExclusiveOwnerThread()){
//判断当前的线程是不是独占锁的线程, 是就将状态值加1(锁的数量可以大于1)--〉请求成功
int var4 = var3 + var1;
if (var4 < 0) {
throw new Error("Maximum lock count exceeded");
}
this. setState(var4);
return true
}
return false;
}
}
AbstractQueuedSynchronizer
public abstract class AbstractQueuedSynchronizer extends AbstractOwnableSynchronizer implements Serializable{
protected final boolean compareAndSetState(int var1, int var2) {
return [unsafe.compareAndSwapInt](http://unsafe.compareandswapint/)(this, stateOffset, var1, var2);
}
public final void acquire(int var1) {
if (!this.tryAcquire(var1) && this.acquireQueued(this.addWaiter(AbstractQueuedSynchronizer.Node.EXCLUSIVE), var1)) {
selfInterrupt();
}
}
//将当前线程封装成Node中, 然后加入等待队列
private AbstractQueuedSynchronizer.Node addWaiter(AbstractQueuedSynchronizer.Node var1) {
AbstractQueuedSynchronizer.Node var2 = new AbstractQueuedSynchronizer.Node(Thread.currentThread(), var1);
AbstractQueuedSynchronizer.Node var3 = this.tail;
如果同步等待队列存在尾节点,将使用cas尝试降尾节点设置var2 ,并将之前的尾节点插入到node之前
if (var3 != null) {
var2.prev = var3;
if (this.compareAndSetTail(var3, var2)) {
var3.next = var2;
return var2;
}
}
//如果不存在尾节点, 或者cas操作失败,就进入正常入队
this.enq(var2);
return var2;
}
//循环直道入队为止
private AbstractQueuedSynchronizer.Node enq(AbstractQueuedSynchronizer.Node var1) {
while(true) {
AbstractQueuedSynchronizer.Node var2 = this.tail;
if (var2 == null) {
//尾节点为空, 创建一个dummy节点,并设置到头节点上, 成功后,将尾节点也指向该dummy节点
if (this.compareAndSetHead(new AbstractQueuedSynchronizer.Node())) {
this.tail = this.head;
}
} else {
var1.prev = var2;
if (this.compareAndSetTail(var2, var1)) {
var2.next = var1;
return var2;
}
}
}
}
//返回true 会中断自己
final boolean acquireQueued(AbstractQueuedSynchronizer.Node var1, int var2) {
boolean var3 = true;
try {
boolean var4 = false;
while(true) {
//获取插入的前一个节点
AbstractQueuedSynchronizer.Node var5 = var1.predecessor();
//第一次成功, 返回的是true ,不需要中断自己
if (var5 == this.head && this.tryAcquire(var2)) {
this.setHead(var1);
var5.next = null;
var3 = false;
boolean var6 = var4;
return var6;
}
if (shouldParkAfterFailedAcquire(var5, var1) && this.parkAndCheckInterrupt()) {
//挂起后操作,会改变变量返回值, 下次循环推出 会调用中断的操作了
var4 = true;
}
}
} finally {
if (var3) {
this.cancelAcquire(var1);
}
}
}
}