Understanding of reentrant locks

reentrant lock

(The following explanation comes from the blog http://http://blog.csdn.net/joker_apple/article/details/52790181)

The so-called reentrant lock refers to the unit of thread. When a thread acquires the object lock, the thread can acquire the lock on the object again, but other threads cannot.

 Both synchronized and ReentrantLock are
 reentrant locks. The meaning of reentrant locks is to prevent deadlocks . The
 implementation principle is achieved by associating a request count for each lock and a thread that owns it.
 When the count is 0, the lock is considered unoccupied. When a thread requests an unoccupied lock, the jvm records the owner of the lock and sets the request counter to 1.
 If the same thread requests the lock again, the count will be incremented;
 each time the occupying thread exits the synchronized block, the count will be decremented. Until the counter reaches 0, the lock is released.  Regarding the reentrancy of the locks of the parent class and the subclass:  the subclass overrides the synchonized method of the parent class, and then calls the method in the parent class.  If there is no reentrant lock at this time, then this code will cause a deadlock.
 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325765452&siteId=291194637