In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to apply StampedLock locks in Java concurrent programming". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
StampedLock:
StampedLock is a new lock in the JDK8 version of the concurrent package. This lock provides three modes of read and write control. When a series of functions that acquire the lock are called, a variable of long type is returned, which is called stamp. This stamp represents the state of the lock. The function of the try series that acquires the lock will return a stamp value of 0 when it fails to acquire the lock. You need to pass in the stamp value that is returned when the lock is acquired when calling the methods that release and convert the lock.
The locks for the three read-write modes provided by StampedLock are as follows:
Write lock witeLock: an exclusive lock or exclusive lock. At some point, only one thread can acquire the lock. When one thread acquires the lock, other threads requesting read and write locks must wait. This is similar to ReentrantReadWriteLock's write lock (except that the write lock here is non-reentrant): it can only be acquired when no thread holds the read lock or write lock. After a successful request for the lock, a stamp variable is returned to indicate the version of the lock. When the lock is released, you need to call the unlockWrite method and pass the stamp parameter to obtain the lock date. And it provides non-blocking tryWriteLock methods.
Pessimistic read lock readLock: a shared lock that can be acquired by multiple threads at the same time without a thread acquiring an exclusive write lock. If a thread already holds a write lock, requests from other threads to acquire the read lock will be blocked, similar to ReentrantReadWriteLock's read lock (except that the read lock here is non-reentrant). Pessimism here means that before operating the data, it will pessimistically think that other threads may have to modify the data they operate, so it is necessary to lock the data first, which is a consideration in the case of reading less and writing more. When the lock is successfully requested, a stamp variable is returned to indicate the version of the lock. When the lock is released, the unlockRead method is called and the stamp parameter is passed. And it provides non-blocking tryReadLock methods.
Optimistic read lock tryOptimisticRead: as opposed to pessimistic lock, it does not set the state of the lock through CAS before manipulating the data, but only passes the bit operation test. If no thread currently holds a write lock, a non-zero stamp version information is simply returned. After obtaining the stamp, you also need to call the validate method to verify whether the stamp is unavailable before the specific operation data, that is, whether another thread holds a write lock during the period from calling tryOptimisticRead to returning stamp to the current time. If so, validate will return 0, otherwise you can use the lock of this stamp version to operate on the data. Because tryOptimisticRead does not use CAS to set the lock state, there is no need to explicitly release the lock. One of the features of this lock is that it is suitable for scenarios with more reads and less writes, because the acquisition of read locks is only checked by bit operations and does not involve CAS operations, so it will be much more efficient, but at the same time, because there is no real lock, it is necessary to copy a copy of the variable to be operated on to the method stack to ensure data consistency, and other writing threads may have modified the data when manipulating the data. What we are dealing with is the data in the method stack, that is, a snapshot, so what is returned at most is not the latest data, but consistency is guaranteed.
StampedLock also supports the conversion of these three locks to each other under certain conditions. For example, long tryConvertToWriteLock (long stamp) expects to upgrade the lock marked by stamp to a write lock
This function returns a valid stamp (that is, the promotion writes the lock successfully) in the following cases:
The current lock is already in write lock mode.
The front lock is in read lock mode, and no other thread is in read lock mode
Currently in optimistic read mode, and the current write lock is available
In addition, the read and write locks of StampedLock are non-reentrant, so you should not call the operation that will acquire the lock before releasing the lock after acquiring the lock, so as to avoid blocking the calling thread. When multiple threads try to acquire a read lock and a write lock at the same time, there is no certain rule for who acquires the lock first. And the lock does not directly implement the Lock or ReadWriteLock interface, but maintains a two-way blocking queue internally.
The following example of managing two-dimensional points is provided in Tonggu JDK8 to understand the concepts introduced above.
Package LockSupportTest;import com.sun.org.apache.bcel.internal.generic.BREAKPOINT;import java.util.concurrent.locks.StampedLock;public class Point_Class {private double xdyy; private final StampedLock sl = new StampedLock (); void move (double deltaX, double deltaY) {long stamp = sl.writeLock (); try {x + = deltaX; y + = deltaY;} finally {sl.unlockWrite (stamp) }} double distanceFromOrin () {long stamp = sl.tryOptimisticRead (); double currentX = x, currentY = y; if (! sl.validate (stamp)) {stamp = sl.readLock (); try {currentX = x; currentY = y;} finally {sl.unlockRead (stamp) }} return Math.sqrt (currentX*currentX + currentY*currentY);} void moveIfAtOrigin (double newX, double newY) {long stamp = sl.readLock (); try {while (x = 0.0 & & y = = 0.0) {long ws = sl.tryConvertToWriteLock (stamp) If (ws! = 0L) {stamp = ws; x = newX; y = newY; break;} else {sl.unlockRead (stamp); stamp = sl.writeLock () } finally {sl.unlock (stamp);}
In the above code, the Point class has two member variables (xfocus y) to represent the two-dimensional coordinates of a point, and three methods to manipulate the coordinate variables. In addition, a StampedLock object is instantiated to ensure the atomicity of the operation.
This is the end of the content of "how to apply StampedLock locks for Java concurrent programming". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.