AQS源码解读(番外篇)——四种自旋锁原理详解(Java代码实现SpinLock、TicketSpinLock、CLH、MCS

@Override

public boolean tryLock() {

return tryAcquire(1);

}

/**

  • 返回获取自旋锁的结果,会自旋一段时间,超时后停止自旋

  • @param time

  • @param unit

  • @return

  • @throws InterruptedException

*/

@Override

public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {

long nanosTimeout = unit.toNanos(time);

if (nanosTimeout <= 0L) {

return false;

}

final long deadline = System.nanoTime() + nanosTimeout;

for(;😉 {

if (Thread.interrupted()) {

//有被中断 抛异常

throw new InterruptedException();

}

if (tryAcquire(1)) {

return true;

}

nanosTimeout = deadline - System.nanoTime();

if (nanosTimeout <= 0L) {

//超时自旋,直接返回false

return false;

}

}

}

@Override

public void unlock() {

tryRelease(1);

}

@Override

public Condition newCondition() {

throw new UnsupportedOperationException();

}

public int getState() {

return state;

}

/**

  • 获取持有锁的当前线程

  • @return

*/

public Thread getExclusiveOwnerThread() {

return exclusiveOwnerThread;

}

/**

  • 获取当前线程重入次数

  • @return

*/

public int getHoldCount() {

return isHeldExclusively() ? getState() : 0;

}

/**

  • 释放锁

  • @param releases

  • @return

*/

protected boolean tryRelease(int releases) {

int c = getState() - releases;

Thread current = Thread.currentThread();

if (current != getExclusiveOwnerThread())

//不是当前线程,不能unLock 抛异常

throw new IllegalMonitorStateException();

boolean free = false;

if (c <= 0) {

//每次减一,c = 0,证明没有线程持有锁了,可以释放了

free = true;

c = 0;

setExclusiveOwnerThread(null);

System.out.println(String.format(“spin un lock ok, thread=%s;”, current.getName()));

}

//排它锁,只有当前线程才会走到这,是线程安全的 修改state

setState©;

return free;

}

/**

  • 获取锁

  • @param acquires

  • @return

*/

protected boolean tryAcquire(int acquires) {

final Thread current = Thread.currentThread();

int c = getState();

if (c == 0) {

//若此时锁空着,则再次尝试抢锁

if (compareAndSetState(0, acquires)) {

setExclusiveOwnerThread(current);

System.out.println(String.format(“spin lock ok, thread=%s;”, current.getName()));

return true;

}

}

//若当前持锁线程是当前线程(重入性)

else if (current == getExclusiveOwnerThread()) {

int nextc = c + acquires;

if (nextc < 0) // overflow

throw new Error(“Maximum lock count exceeded”);

//重入

setState(nextc);

System.out.println(String.format(“spin re lock ok, thread=%s;state=%d;”, current.getName(), getState()));

return true;

}

return false;

}

/**

  • 判断当前线程是否持有锁

  • @return

*/

protected final boolean isHeldExclusively() {

return getExclusiveOwnerThread() == Thread.currentThread();

}

protected void setState(int state) {

this.state = state;

}

protected void setExclusiveOwnerThread(Thread exclusiveOwnerThread) {

this.exclusiveOwnerThread = exclusiveOwnerThread;

}

protected final boolean compareAndSetState(int expect, int update) {

retu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值