The read-write lock determination ReentrantReadWriteLock

A. Read-write lock is how to achieve?

  AQS succession, then the AQS by converting binary state, is divided into upper 16 bits and lower 16 bits to distinguish. High 16 indicates a read state, the lower 16 bits write state.

II. Analytical representation (level 16)

  Assuming a state value S

  a. Write Lock

    When a write lock is determined by K = S & 0X0000FFFF, it will erase all high, then only the lower 16 bits, and then determines whether or not K is greater than 0, K> 0, represents write locks, not vice versa.

  b. a read lock

    When a read lock, by K = S >>> 16, we can see that an unsigned right shift (int are signed, one is to prevent the upper, right upper S.1), this time to the right, high bit 0 , the lower 16 bits of write lock determination removed. Then it is determined whether K is greater than 0, K> 0, there is a read lock, not vice versa.

The figure is operation when the acquired read-write lock. Methods of tryAcquire and tryAcquireShare.

 

 

 

  

 

Guess you like

Origin www.cnblogs.com/lifacheng/p/11614164.html