Chapter 7 ReentrantLock summary, reentrantlock
Common Methods:
Int a = 12; // Note: This is usually set to a class variable, for example, the segment lock in the Segement and the global lock final ReentrantLock lock in the copyOnWriteArrayList = new ReentrantLock (); lock. lock (); // get the lock try {a ++; // business logic} catch (Exception e) {} finally {lock. unlock (); // release the lock}
1. Unfair lock acquisition steps lock ()
CAS-based attempt to set the state (number of locks) from 0 to 1
A. If the setting is successful, set the current thread to an exclusive lock;
B. If the setting fails, the number of locks will be obtained again,
B1. If the number of locks is 0, set the state (number of locks) from 0 to 1 based on CAS. If the setting is successful, set the current thread to the exclusive lock thread;
B2. If the number of locks is not 0 or the attempt above fails, check whether the current thread is an exclusive lock thread. If yes, the current number of locks is plus 1;
If not, the thread is encapsulated in a Node and added to the waiting queue. Wait until it is awakened by the previous thread node.
2. Steps for obtaining a fair lock ()
Obtain the number of locks at a time,
B1. If the number of locks is 0, if the current thread is waiting for the header node in the queue, the CAS-based attempt is to set the state (number of locks) from 0 to 1. If the setting is successful, sets the current thread to an exclusive lock thread;
B2. If the number of locks is not 0 or the current thread does not wait for the first node in the queue or the attempt above fails, check whether the current thread is an exclusive lock thread. If yes, the current number of locks is + 1. If not, the thread is encapsulated in a Node and added to the waiting queue. Wait until it is awakened by the previous thread node.
3. Unlock steps
1) obtain the current number of locks, then use the number of locks minus the number of locks (1 here), and finally obtain the result c.
2) checks whether the current thread is an exclusive lock thread. If not, an exception is thrown.
3) If c = 0, the lock is successfully released, the current exclusive thread is set to null, the number of locks is set to 0, and true is returned.
4) if c! = 0 indicates that the lock failed to be released. If the number of locks is set to c, false is returned.
5) if the lock is released successfully, wake up a non-canceled node closest to the first node.
4. Comparison of four lock acquisition methods of ReentrantLock
/* ** Get a lock *: * 1. If the lock is not held by any thread (including the current thread), the lock is obtained immediately. The number of locks = 1, then execute the corresponding business logic * 2. If the current thread is holding this lock, the number of locks is + 1, then execute the corresponding business logic * 3. If the current lock is held by another thread, the current thread is in sleep state until the lock is obtained and the current thread is awakened, number of locks = 1, and then execute the corresponding business logic * In short, get the lock. If the lock cannot be obtained, the current thread is blocked until the lock can be obtained and obtained successfully. */Public void lock () {sync. lock (); // call the lock () method of NonfairSync (unfair lock) or FairSync (fair lock)}/*** lockInterruptibly (): * 1. Obtain the lock if the current thread is not interrupted. * 2. if the result is obtained successfully, the method ends. * 3. If the lock cannot be obtained, the current thread is blocked until the following situation occurs: * 1) the current thread (after being awakened) successfully acquires the lock * 2) the current thread is interrupted by other threads */public void lockInterruptibly () throws InterruptedException {sync. acquireInterruptibly (1);}/*** four points Note: * 1. If other threads do not hold this lock, return true immediately, set the number of locks to 1*2. If the current thread already holds this lock, then the number of locks + 1 will be returned immediately true * 3. If other threads occupy this lock, this method returns false immediately * 4. Note: This lock is a non-fair lock (that is, it cannot be used in a fair lock Policy) */public boolean tryLock () {return sync. nonfairTryAcquire (1);}/*** the lock is not occupied by other threads at a certain time within the specified time and the current thread is not interrupted. Obtain this lock ** 1) if no other thread occupies the lock, the lock is obtained and true is returned immediately. The number of locks + 1*2) if the current thread already holds the lock, the number of locks is + 1, returns true immediately * 3) if the lock is held by another thread, the current thread is blocked until the following three conditions occur: * A, the current thread (after being awakened) successful lock acquisition * B. Other threads interrupt the current thread * C. The specified time-out (if the time is <= 0, it will not wait) ** if the lock is used for fair lock: if other threads are waiting, even if no other thread occupies the lock, the current thread will not obtain this lock */public boolean tryLock (long timeout, TimeUnit unit) throws InterruptedException {return sync. tryAcquireNanos (1, unit. toNanos (timeout ));}