In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the role of StampedLock in Java1.8, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
StampedLock divides reading into pessimistic reading and optimistic reading. Pessimistic reading is equivalent to ReadWriteLock reading, while optimistic reading is not blocked when a thread writes shared variables, and optimistic reading is unlocked. Therefore, the performance of unlocked is definitely better than that of locked, so it is more efficient in the case of large parallel reading!
The usage of StampedLock is slightly different. When acquiring locks and optimistic reads, a stamp is returned. When unlocking, the stamp needs to be passed in to verify whether the shared variables have been written by other threads. Let's take a look at the official example.
When the operation on it is optimistic, if a write operation modifies the shared variable, the optimistic read is upgraded to a pessimistic read lock, so as to avoid the cycle of optimistic reading waiting for the release of the write lock and the waste of CPU resources. So this is recommended when we use StampedLock.
It seems that the performance of StampedLock is better than that of ReadWriteLock lock, so can you discard ReadWriteLock with StampedLock?
No, StampedLock is not a reentrant lock, so reentrant is not supported, and StampedLock does not support conditional variables, that is, no Condition. If the thread uses writeLock () or readLock () to acquire the lock and the thread is interrupt () before it finishes execution, it will cause the CPU to soar. A pit.
Let's take a look at the source code
First of all, there is an infinite loop, and then if (interruptible & & Thread.interrupted ()) has learned that the interruptible parameter of the call is false, so Thread.interrupted () will not be executed and cancelWaiter will not be called, so the loop will continue, and the CPU utilization will rise.
So if you want to use the interrupt function, you have to use readLockInterruptibly () or writeLockInterruptibly () to acquire the lock.
This is what the role of StampedLock in Java1.8 is, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.