In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Java in how to achieve non-reentrant lock and reentrant lock, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Basic knowledge
Wait () method and notify () method of Java multithreading
These two methods are presented and used in pairs. To execute these two methods, there is a premise that the current thread must acquire the monitor of its object (commonly known as "lock"), otherwise an IllegalMonitorStateException exception will be thrown, so these two methods must be called in the synchronous block code.
Wait (): blocks the current thread
Notify (): evokes threads blocked by wait ()
Non-reentrant lock
The so-called non-reentrant lock means that if the current thread has acquired the lock by executing a method, it will not be blocked when it attempts to acquire the lock again in the method. We try to design a non-reentrant lock:
Use the lock:
The current thread executes the print () method to get the lock first, then the doAdd () method cannot execute the logic in doAdd (), and the lock must be released first. This example is a good example of a non-reentrant lock.
Reentrant lock next, we design a reentrant lock
The so-called reentrant means that a thread can enter the synchronization code block of the lock it already has.
We design two threads to call the print () method. The first thread calls the print () method to acquire the lock and enters the lock () method. Because the initial lockedBy is null, it does not enter the while and suspends the current thread, but increments the lockedCount and records the lockBy as the first thread. Then the first thread enters the doAdd () method, which does not enter while and hangs because of the same process, and then increments lockedCount, when the second thread tries to lock, because of isLocked=true, he will not acquire the lock until the first thread calls unlock () twice to decrement lockCount to 0, marking isLocked as false.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.