源码调用
2021-03-24 23:59:39 26 举报
AQS内部调用
作者其他创作
大纲/内容
parkAndCheckInterrupt()
尝试获取一次,若state=0则获得锁,若state不为0且当前独占线程=current,则state+1,表示重入
acquireShared(1)
acquire(1);
state改变是volatile的
AQS=cas+volatile
ReentrantLock的非公平锁的lock方法调用实现
nonfairTryAcquire(acquires)
cas失败
unlock就比较简单:1.将state-1,2.如果state==0则完全释放锁3.设置独占线程为null4.state=0则调用LockSupport.unpark
readLock-->lock-->AQS
selfInterrupt();
cas成功设置独占线程
线程阻塞的核心
调用的LockSupport.park
writeLock
/** Returns the number of shared holds represented in count */ static int sharedCount(int c) { return c >>> SHARED_SHIFT; } /** Returns the number of exclusive holds represented in count */ static int exclusiveCount(int c) { return c & EXCLUSIVE_MASK; }
tryAcquire(arg)
setExclusiveOwnerThread(currentThread())
tryAcquireShared
非公平锁每个线程进来都先cas,unsafe中cas AQS中的state能否成功
addWaiter 将当前线程入队,修改head和tail都是cas操作
ReentrantLock-->AQS-->NonFairAQSlock()
0 条评论
下一页
为你推荐
查看更多