---restore content starts---
Reentrantlockextends objectimplements Lock, Serializable
One reentrant Mutex ...
Multithreaded access programs that use fair locks may show lower throughput performance than those using the default settings (that is, slow, often much slower), but the gap between time and "get lock" is smaller and ensures no starvation.
However, it is important to note that the fairness of the lock does not guarantee the fairness of the thread scheduling. That is, a thread that uses a fair lock may successfully acquire a lock multiple times when another active state thread is not running and is not currently getting a lock. It is also important to note that the Trylock () is not timed.
The method does not follow the fairness setting, even if there are other threads waiting, as long as the lock is available trylock () will succeed.
It is recommended that you always call the lock () method followed by a try{} code block in the actual operation, with the following typical structure:
Class X { private final reentrantlock lock = new Reentrantlock (); // ... public void M () { lock.lock (); Block until condition holds try { //... method body } finally { lock.unlock () }}}
In addition to implementing the lock interface, the class defines the islocked and Getlockqueuelength methods, as well as some related methods of protected access adornments, which may be useful for monitoring and monitoring locks.
Java.util.concurrent.locks.ReentrantLock