Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How is ReentrantLock designated as a fair lock

Shulou Source: shulou.com Published: 2022-06-01 23:16:16 10月02日 Update

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)

Tags: Learning efficiency more threads results help practicality balance and then parameters multiple that is feelings articles methods time right and wrong theory knowledge articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno Shulou Information MariaDB Xiaomi