In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the difference between java fair lock and unfair lock". In daily operation, I believe that many people have doubts about the difference between java fair lock and unfair lock. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the difference between java fair lock and unfair lock". Next, please follow the editor to study!
Fair lock: indicates that the order in which the thread acquires the lock is based on the thread startup order, that is, first come, first served, first out.
Unfair lock: indicates that the acquisition lock is acquired at random, and starting first does not guarantee that the lock will be acquired first.
Let's use two examples to demonstrate fair locking and unfair locking.
Fair lock
We see that the running order of the output thread is the same as the order in which the thread acquires the lock. This is the characteristic of fair lock, first come, first served. Let's take a look at unfair locks.
Unfair lock
We just need to change the parameter of the Userinfo object in the test class to true to false.
We see that there is no rule in the order in which threads acquire locks and the order in which threads execute, that is, unfair locks, and there is no guarantee that the thread that executes first will be able to get the lock first.
The difference between fair locks and unfair locks:
The performance of fair lock is lower than that of unfair lock.
The default construction parameter of the ReentrantLock class is false, that is, an unfair lock by default.
At this point, the study on "the difference between java fair lock and unfair lock" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.