Guest User

Untitled

a guest
Dec 11th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. final boolean nonfairTryAcquire(int acquires) {
  2. final Thread current = Thread.currentThread();
  3. int c = getState();
  4. if (c == 0) { // 0 表示锁可用
  5. if (compareAndSetState(0, acquires)) { // CAS获取相应数量的锁
  6. setExclusiveOwnerThread(current); // 记录当前线程为占用排他锁的线程,用以让该线程可以重入地再次获取
  7. return true;
  8. }
  9. }
  10. else if (current == getExclusiveOwnerThread()) { // 允许占有排他锁的线程重入
  11. int nextc = c + acquires;
  12. if (nextc < 0) // overflow
  13. throw new Error("Maximum lock count exceeded");
  14. setState(nextc); // 更新锁被获取的数量
  15. return true;
  16. }
  17. return false;
  18. }
Add Comment
Please, Sign In to add comment