In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the java how to achieve reentrant spin lock related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this java how to achieve reentrant spin lock article will have a harvest, let's take a look.
Description
1. It means that the thread trying to acquire the lock will not be blocked, but will acquire the lock through a loop.
2. Advantages: reduce the consumption of context switching.
Cons: cycle consumption of CPU.
Example
Public class ReentrantSpinLock {private AtomicReference owner = new AtomicReference (); / / number of reentrants private int count = 0; / / Lock public void lock () {Thread current = Thread.currentThread (); if (owner.get () = = current) {count++; return } while (! owner.compareAndSet (null, current)) {System.out.println ("--I'm spinning--");}} / / unlock public void unLock () {Thread current = Thread.currentThread () / / only threads holding locks can unlock if (owner.get () = = current) {if (count > 0) {count--;} else {/ / there is no CAS operation here, because there is no competition, because only thread holders can unlock owner.set (null). } public static void main (String [] args) {ReentrantSpinLock spinLock = new ReentrantSpinLock (); Runnable runnable = ()-> {System.out.println (Thread.currentThread (). GetName () + "start trying to acquire spin lock"); spinLock.lock () Try {System.out.println (Thread.currentThread (). GetName () + "spin lock acquired"); Thread.sleep (4000);} catch (InterruptedException e) {e.printStackTrace ();} finally {spinLock.unLock () System.out.println (Thread.currentThread (). GetName () + "spin lock released");}}; Thread thread1 = new Thread (runnable); Thread thread2 = new Thread (runnable); thread1.start (); thread2.start () }} this is the end of the article on "how to implement reentrant spin locks in java". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to achieve reentrant spin locks in java". If you want to learn more, you are welcome to follow the industry information channel.
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.