In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand Java locks". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand Java locks.
Spin?
Spin lock
If the lock is not available at this time, it does not immediately enter the blocking state, but is willing to wait for a period of time.
If the loop does not get the lock for a certain number of times, then it will enter the blocking state, and the number of cycles can be artificially specified.
Spin lock?
One day when I went to the whole family to buy coffee, the waiter said that unfortunately, the front coffee machine was broken and I had to wait for 10 minutes. There happened to be nothing urgent, so just wait and sit in the rest area for 10 minutes (nothing else was done). This is the spin lock (idling for a while)
Think it's a waste of time? If you have waited 15 minutes and haven't fixed it, you may not want to wait any longer (15 minutes is the maximum spin waiting time set).
It is said above that the number of spin lock cycles is artificially specified, while the adaptive spin lock is awesome. It does not need to specify the number of cycles artificially, it will determine how many times to cycle, and the number of times each thread may cycle is also different.
If the thread has acquired a lock before, or often gets a lock, it will judge by itself that the probability of acquiring the lock again is high, and the number of loops will be higher; if the thread has not acquired the lock before, it will not be sure, for fear of consuming CPU, the number of loops will be smaller.
It solves the problem of "uncertain lock competition time", but it is not necessarily set by itself.
Adaptive rotation lock?
Or go to the whole family to wait for the coffee chestnut ~ if you wait for 5 minutes, and you can't fix it in 10 minutes, you won't wait any longer (the number of cycles is small)
If you have been waiting for 10 minutes, the waiter says that you are very sorry, it will be available in 1 minute, and you are not in a hurry. You have been waiting for 10 minutes, so just wait a little longer (with a large number of cycles).
This is a simple code implementation of spin lock:
Public class SpinLock {private AtomicReference cas = new AtomicReference (); public void lock () {Thread current = Thread.currentThread (); / / using CAS while (! cas.compareAndSet (null, current)) {/ / DO nothing}} public void unlock () {Thread current = Thread.currentThread (); cas.compareAndSet (current, null) }}
Analyze it a little bit.
The lock () method uses CAS. When the first thread An acquires the lock, it can acquire it successfully and will not enter the while loop.
If thread A does not release the lock at this time, and another thread B comes to acquire the lock, it will enter the while loop because it does not meet the CAS.
Thread B then continues to determine whether the CAS is satisfied, and it cannot acquire the lock until thread A calls the unlock method to release the lock.
There are mainly the following problems:
If a thread holds the lock for too long, it will cause other threads waiting for the lock to enter the loop and consume CPU. Improper use will result in extremely high CPU utilization.
Fairness cannot be guaranteed by itself, that is, it cannot satisfy the thread with the longest waiting time to acquire the lock first. Unfair locks have the problem of "thread hunger".
There is no guarantee of reentrancy. Based on the spin lock, the lock with fairness and reentrant property can be realized.
Let's talk about these later in more detail.
Spin lock Vs blocking lock
Blocked chestnuts ~
Go to a popular restaurant for dinner. When you get to the door, the seats at the door are full of people. What's wrong with this? The waiter said, you can first take a number ~ scan a QR code on the ticket and follow us. When it's your turn, there will be a prompt in the service number. )
Then you first take a number to visit the surrounding shops, and when it is your turn, you will receive a service reminder message on your phone. When it is time for you to go, you can enter the store.
This is the process of blocking.
What about spin?
If you don't do anything else, just wait there, just like waiting in line at the supermarket to check out. If you walk away, no one will let you know, so you have to wait in line again. You need to check if you have access to shared resources at all times.
Here's an interruption:
Blocking or waking up a CPU thread requires the operating system to switch between Java states, which takes processor time.
Let's take a look at the comparison between spin and blocking.
The lock state of only rising but not falling.
There are four main states of lock: "no lock state, partial lock state, lightweight lock state, heavy lock state".
In fact, none of these four states are locks in the Java language, but are optimized by Jvm to improve the efficiency of lock acquisition and release (when using synchronized).
They will gradually upgrade with the fierce competition, and they are irreversible.
The upgrade process goes like this:
Bias lock-> lightweight lock-> heavyweight lock
About unlocked ~
If a method does not involve sharing data in the first place, it naturally does not need any synchronization measures to ensure correctness, so some code will be inherently thread-safe.
It does not lock the resource, all threads can access and modify the same resource, but only one thread can modify it successfully.
The CAS algorithm, or compare and swap (compare and Exchange), is a well-known lock-free algorithm.
Let's compare the status in detail.
I know you want chestnuts.
You often go to a store and sit in the same place to eat, and the boss has remembered you. Every time you go, as long as there are not many customers in the store, the boss will save that seat for you. This seat is your "bias lock". You are the only thread to use at a time.
One day when you go there, the store is full and your seat is taken by someone else, so you have to wait (enter the competitive state), and that seat will be upgraded to a "lightweight lock".
If that seat is particularly good (the view near the window is the best, and you can enjoy the moon across the river) every time you arrive, several other people will also want to grab that seat and don't eat if you don't sit in that seat.
Is it easy to understand?
Share or exclusively?
Or professionally talk about the concept ~ (please click the picture on the mobile phone to enlarge ~)
And chestnuts.
Every week, all the members of the group should fill out a weekly report form.
If everyone can add a write lock when opening it, that is, when you are writing, no one else can modify it, this is the exclusive lock (write lock)
But this form can be opened at the same time, see the contents of the table (read the data), the person who is changing the data can add a shared lock to the form, then the lock is the shared lock.
At this point, I believe that you have a deeper understanding of "how to understand Java locks", you might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.