preface

Last we analyzed ReentrantLock fair lock access and release source code, this look at the source code of non-fair lock is much easier, the last article did not see the suggestion to look at the source code of fair lock analysis.

1. Get an unfair lock

The following is the source code for unfair lock acquisition, in the NonfairSync class of ReentrantLock. Java.

final void lock(a) {
	// CAS determines whether the lock is idle
     if (compareAndSetState(0.1))
     	// Set current thread to the holder of the lock
         setExclusiveOwnerThread(Thread.currentThread());
     else
         acquire(1);
 }
Copy the code

Acquire () in AbstractQueuedSynchronizer (AQS) defined in the abstract class, its source code is as follows:

 // Notice that the tryAcquire method is implemented by the fair and non-fair locking classes themselves
 The addWaiter, acquireQueued, and selfInterrupt methods are implemented by AQS itself
 public final void acquire(int arg) {
     if(! tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); }Copy the code

1.1. TryAcquire method

TryAcquire () is implemented in the NonfairSync class of ReentrantLock. Java, source code is as follows:

protected final boolean tryAcquire(int acquires) {
     return nonfairTryAcquire(acquires);
}
Copy the code

NonfairTryAcquire () is implemented in the Sync class of reentrantLock. Java, source code is as follows:

final boolean nonfairTryAcquire(int acquires) {
      final Thread current = Thread.currentThread();
      int c = getState();
      if (c == 0) {
      	  // If you are careful, you may have noticed that there is one line missing from the fair lock
      	  // The missing line is:! Hasqueuedtoraise (), to determine whether you are the first in the waiting queue
          if (compareAndSetState(0, acquires)) {
              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;
 }
Copy the code

2. Release an unfair lock

Releasing an unfair lock is the same as releasing a fair lock, which is not repeated here. If you forget, you can go back to the fair lock release method.

3. The difference between fair and unfair locks

  1. The difference between a fair lock and an unfair lock is the mechanism by which the lock is acquired.
  2. Fair lock A lock can be acquired only if the current thread is at the head of the queue and the lock is idle.
  3. Unfair lock As long as the lock is free, the current thread can directly acquire the lock.
  4. An unfair lock is queued to obtain the lock after the attempt fails, like a fair lock.

conclusion

This article concludes the source code analysis of unfair locks, and concludes the difference between fair and unfair locks in “exclusive lock” ReentrantLock. The next article will analyze ReentrantReadWriteLock.