Fair lock and unfair lock in java

There are two ways for the thread locking mechanism in java. One is to use the synchronize keyword to modify the code block or method, and the other is the lock lock mechanism implemented under the juc package after jdk1.5. This article mainly discusses the juc package The following reentrant lock ReentrantLock.

Method to realize:

  • Fair lock:

Lock lock = new ReentrantLock (true);

  • Unfair lock
    Lock lock = new ReentrantLock ();

Features of fair lock and unfair lock:

Fair lock means that when multiple threads are waiting for the same lock, they must acquire the lock at the same time according to the order of applying for the lock.

The benefit of fair locks is that threads waiting for locks will not starve to death, but the overall efficiency is relatively low; the benefit of unfair locks is that the overall efficiency is relatively higher, but some threads may starve or wait for locks very early, but It takes a long time to acquire the lock. The reason is that fair locks are queued to obtain locks strictly in the order of the request, and can be preempted when they are not fair locks, that is, if a thread needs to acquire a lock at a certain time, and the lock is available at this time, then this thread It will directly preempt, and the thread blocked in the waiting queue will not be awakened at this time;

Fair lock:

FairSync is that after the current resource is locked, all other request threads are placed in the queue in the order of the request. When the lock is released and released, then one by one is strictly locked according to the first-in first-out principle.
For example, there is only one cash register in the supermarket to pay after shopping in the supermarket. This cash register can only serve one customer. When there are too many people paying for the bill, everyone needs to come in line and pay in line.

Unfair lock:

NonfairSync Non-fair means that you are under control. I will try to lock the shared resources first. If the lock is successful, other threads will be blocked (because other threads are queued in the queue, this time is particularly overbearing and appears not to Fair), if it is already locked on the shared resource, it is necessary to judge whether it can be locked at this time. Two attempts to lock the lock have failed and it is useless to be overbearing. You can only honestly go to the end of the queue to queue !
Or go to the supermarket to pay after shopping, there is only one cashier, this cashier can only serve one customer When there are so many people paying, everyone is waiting in line. At this time, there was a strong man, relying on his tall and vain game rules, he ran directly to the cash register to see if anyone was paying, and if no one was paying, he jumped in line to pay. If after two glances someone is still paying the bill, it's time to line up at the end of the line.

Reference article: https://blog.csdn.net/weixin_43142697/article/details/88827131
Reference article: https://blog.csdn.net/u013256816/article/details/51204385

Published 39 original articles · won praise 1 · views 4620

Guess you like

Origin blog.csdn.net/thetimelyrain/article/details/100984865