How is ReentrantLock designated as a fair lock
This article mainly introduces "how to designate ReentrantLock as a fair lock". In daily operation, I believe many people have doubts about how to designate ReentrantLock as a fair lock. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to designate ReentrantLock as a fair lock". Next, please follow the editor to study!
The default synchronized is an unfair lock. Multiple threads access the same resource, and one of them gets the lock. Wait a second and wait a day, maybe the next second or wait a second to get the lock first. )
Fair lock: it is the one who waits for a long time to get the lock. Fair lock efficiency is relatively low, but more fair. Unfair lock is more efficient, but unfair.
Public class ReentrantLock5 extends Thread {private static ReentrantLock lock=new ReentrantLock (true); / / parameter true indicates fair lock. Compare the output result public void run () {for (int iLock 0; I)