Java Concurrency exclusive lock of a more equitable locking and non-locking fair

Java Concurrency exclusive lock of a more equitable locking and non-locking fair

Lock lock fair and unfair understand:

In the previous article, we know that the unfair lock. In fact, Java, there are still a fair lock it. Fair word how to understand it? And our understanding of reality is the same. We queued up in line with the principle of first come, first obtained in the queue, regardless of status or low, equal treatment. This is what we in real life fair. Everyone loves fair. But the default is non-fair in Java, why?

The main contents are: fair lock of real-life understanding; lock fair presentation; Why in Java (compare unfair lock fair locks) is non-default fair locks

Benpian is: Part IV "Kaige (Kaige Java kagejava) concurrent programming learning" "Lock series" tutorial series: "Java contract under lock and learning Part V: fair locks understand and compared with non-equity locks . "

Life examples:

The same went example of the ATM machine to withdraw money. Suppose there are three people use the ATM to withdraw money. Passerby will not use ATM, took their own way 5min, then finally learned how to use, but has forgotten the password. Call family counseling takes 1min. When a passerby operation is completed, then line up behind the two men in turn operate in this way is who to who should operate, after the operation is completed next people can operate, whether rich or poor high or low, whether you take 100 or take 1W, 1W take back people who take 100, it is necessary lined up waiting, this looks very fair, whether high or low, we turn the operation, this mode of operation station then, it is fair locks in multi-threaded operating point of view.

After the passerby total elapsed time 6min, Lu Renyi and passer-by operation takes two people 3min. That is, three people total elapsed time 9min. Why would this happen? Because Duzhe a passerby. It has been occupied lock resources. During the passerby operation, others can only wait in line. If you do not operate passerby, Lu Renbing came in behind him to jump the queue to ask passers-by, they can jump the queue first operating ATM, while the church passerby. If A consent, then C, can operate after completion of armor can learn to operate others, there may also be a passerby 2min operation is completed. This time, three of the total time is 5min; if A does not agree propane operations jump the queue, then the propane can only return to the original position, were waiting. This mode of operation multithreading stand point of view, then, the passerby to call home mode and time consuming and is looking at CPU context switching time consuming. After Lu Renbing jump the queue to obtain ATM resources, this operation can be looked at non-fair, because C's entry time is later than Lu Renyi, but the operation of the first propylene. But the total elapsed time from the last three of view, Lu Renbing jump the queue, the efficiency is improved. This operation in Java concurrency, called unfair lock.

It should be noted that, whether it is explicit or implicit locks locks are non-default fair. Because non-equity can enhance the throughput of the system.

Unfair lock definition:

First try to get a thread synchronization status of the operation (propane to try to jump the queue), if acquired, to the shared variable operation (A C's consent to jump the queue, propane operation on ATM machines), if not get it then line up.

Use Method Two: Exclusive fair presentation

Requirements: consistent with the results and order a thread console print. code show as below:

0fdzKJjVdPU


At this time the only difference codes and the codes shown above: in the example of the lock is a parameter set true.

operation result:

0fdzKK7Cp4i


From the operating results, we found that the console print out the order to acquire the lock and the lock call when the order is the same, we have reached the expected results. However, it is still only one thread operation, such as after the operation is completed the thread releases the lock, other threads can then acquire the lock.

Fairer locking and non-locking of the fair

problem:

Why concurrent design guru Doug Lea ReentrantLock the default mode is non-fair?

In fact, to answer this question, we need to compare the different equity and non-equity lock lock. Let's take a look at ATM machines operate in a fair and non-locking lock fair scene, as shown below:

0fdzKKQE9cu


Fair locks: Everyone line up, if a thread Duzhao the (passers-by), other threads can only wait for this. Eventually, the three-threaded operation is completed, the total time 9min.

非公平锁情况下:多个线程操作的共享资源的时候,发现共享资源还没有被锁定(路人甲还在摸索过程),就尝试插队(路人丙尝试和甲沟通,先插队操作并教会甲),如果插队成功(甲同意了),就操作共享资源(丙先操作ATM机);如果插队失败(甲不同意),接着排队(丙回到队伍中排队)。如果插队成功,最终耗时:5min.

从中我们可以看出公平锁和非公平锁的优缺点了。

优缺点比较:

非公平锁:

优点:效率高;缺点:容易导致线程“饥饿”。当多个线程使用非公平的话,有可能有一个线程一直就获取不到竞争权,导致这个线程会“饥饿而死”。

适用场景:

如果在不考虑TPS(单位时间内成功完成的次数)作为唯一考量指标的场景下,可以使用非公平锁来操作,因为非公平锁能提高系统的吞吐量;

公平锁:

优点:避免了线程的“饥饿”;缺点:性能相对于公平锁会差很多。

0fdzKLr1DyS

wx.jpg

                                                                        欢迎来聊



Guess you like

Origin blog.51cto.com/kaigejava/2484891