Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- final boolean nonfairTryAcquire(int acquires) {
- final Thread current = Thread.currentThread();
- int c = getState();
- if (c == 0) { // 0 表示锁可用
- if (compareAndSetState(0, acquires)) { // CAS获取相应数量的锁
- setExclusiveOwnerThread(current); // 记录当前线程为占用排他锁的线程,用以让该线程可以重入地再次获取
- return true;
- }
- }
- else if (current == getExclusiveOwnerThread()) { // 允许占有排他锁的线程重入
- int nextc = c + acquires;
- if (nextc < 0) // overflow
- throw new Error("Maximum lock count exceeded");
- setState(nextc); // 更新锁被获取的数量
- return true;
- }
- return false;
- }
Add Comment
Please, Sign In to add comment