Java Concurrency: reentrant lock ReentrantLock

ReentrantLock is a reentrant mutex, it is not synchronized keyword as implicit support for the re-entry, but can make a thread (in different ways) Repeat Repeat lock the resource without being blocked.

ReentrantLock Java class diagram:

Sync abstract class which inherits static inner AQS, visible ReentrantLock is achieved by the lock acquisition and release of a combination of a custom synchronizer.

ReentrantLock constructor can control the fairness of the performance of the lock:

1     public ReentrantLock(boolean fair) {
2         sync = fair ? new FairSync() : new NonfairSync();
3     }

Fair enough locks to reduce the "hunger" probability, the longer the wait the more the priority of a request to meet.

Fairness or not is for the purposes of acquiring the lock, a lock if fair, then lock acquisition absolute chronological order should comply with the request, subject to availability.

Fairness lock each time from the synchronization queue to obtain the first node to the lock, the lock will appear instead of the fairness of the situation a continuous thread to acquire the lock, in particular, has just released the lock thread gets the synchronization status in the sub chance of bigger, so that other threads waiting for synchronization in the queue.

On the thread context switching aspects of the system, the number of non fairness lock switch will be significantly less than the fairness of the lock because of the relatively greater fairness lock switching overhead, but little thread switch to ensure greater throughput.

The basic structure of the class code ReentrantLock:

. 1  public  class of ReentrantLock the implements Lock, the java.io.Serializable {
 2      Private  Final Sync Sync;
 . 3  
. 4      // default constructor with no arguments, the default non-fair lock 
. 5      public of ReentrantLock () {
 . 6          Sync = new new NonfairSync ();
 . 7      }
 . 8  
. 9      // constructor with no arguments, the decision is fair or unfair lock latch 
10      public of ReentrantLock ( Boolean fair) {
 . 11          Sync = fair? new new FairSync (): new new NonfairSync ();
 12 is      }
 13 is 
14      // abstract base class inherits the AQS, fair fair locking and non-locking inherited class, respectively, and achieve their lock () method 
15      abstract  static  class Sync the extends AbstractQueuedSynchronizer {
 16          abstract  void lock ();
 . 17          // omitted .. 
18      }
 . 19      
20 is      // unfair achieve lock 
21 is      static  Final  class NonfairSync the extends Sync {...}
 22 is      
23 is      // fair locks for 
24      static  Final  class FairSync the extends Sync {....}
 25     
26 is      //Locks for, depending on the specific implementation calls subclass 
27      public  void Lock () {
 28          sync.lock ();
 29      }
 30  
31 is      // interrupt response acquiring the lock 
32      public  void lockInterruptibly in () throws InterruptedException {
 33 is          sync.acquireInterruptibly (. 1 ) ;
 34      }
 35  
36      // attempt to acquire the lock, the lock default non-fair way to achieve 
37 [      public  Boolean tryLock () {
 38 is          return sync.nonfairTryAcquire (. 1 );
 39      }
 40  
41 is      // timeout acquire lock
42 is      public  Boolean tryLock ( Long timeout, TimeUnit Unit)
 43 is              throws InterruptedException {
 44 is          return sync.tryAcquireNanos (. 1 , unit.toNanos (timeout));
 45      }
 46 is  
47      // release lock 
48      public  void UNLOCK () {
 49          sync.release (1 );
 50      }
 51 is  
52 is      // create the lock condition (understood from Condetion, is to create a queue) 
53 is      public for condition condition newCondition () {
 54 is          return sync.newCondition ();
 55      }
56  
57      // omitted .... 
58 }

Method ReentrantLock release the lock, abstract static inner classes Sync, which will examine the current thread synchronization state value (repeatedly acquire a lock state value increases), to reduce the synchronization state value upon release synchronization state, if the lock acquisition of the n times, then the first (n-1) times return is false, only the synchronous state is fully released (c 0), will occupy the resources of the thread is set to null, and returns true.

 1     protected final boolean tryRelease(int releases) {
 2             int c = getState() - releases;
 3             if (Thread.currentThread() != getExclusiveOwnerThread())
 4                 throw new IllegalMonitorStateException();
 5             boolean free = false;
 6             if (c == 0) {
 7                 free = true;
 8                 setExclusiveOwnerThread(null);
 9             }
10             setState(c);
11             return free;
12         }

 

Guess you like

Origin www.cnblogs.com/magic-sea/p/11586548.html