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 relevant knowledge of how to use java read-write lock and what its advantages are, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use java read-write lock and what its advantages are. Let's take a look at it.
Foreword:
As the name implies, a Readers-Writer Lock is a lock divided into two parts: a read lock and a write lock, in which the read lock allows multiple threads to acquire at the same time, because the read operation itself is thread-safe, while the write lock is mutually exclusive, multiple threads are not allowed to acquire write locks at the same time, and write and read operations are mutually exclusive. To sum up, the characteristics of read-write lock are: read-write mutual exclusion, read-write mutual exclusion, write-write mutual exclusion.
1. Use of read-write locks
In the Java language, read-write locks are implemented using the ReentrantReadWriteLock class, where:
ReentrantReadWriteLock.ReadLock stands for read lock, which provides lock method for locking and unlock method for unlocking.
ReentrantReadWriteLock.WriteLock stands for write lock, which provides lock method for locking and unlock method for unlocking.
Its foundation is shown in the following code:
/ / create a read-write lock final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock (); / / obtain a read-write lock final ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock (); / obtain a read-write lock final ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock (); / / use readLock.lock () for a read-write lock; try {/ / business code.} finally {readLock.unlock ();} / write lock uses writeLock.lock (); try {/ / business code.} finally {writeLock.unlock () } 1.1 Reading is not mutually exclusive
Multiple threads can acquire the read lock at the same time, which is called read non-mutual exclusion, as shown in the following code:
/ / create read-write lock final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock (); / / create read lock final ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock (); Thread T1 = new Thread (()-> {readLock.lock (); try {System.out.println ("[T1] get read lock."); Thread.sleep (3000);} catch (InterruptedException e) {e.printStackTrace () } finally {System.out.println ("[T1] release read lock."); readLock.unlock ();}}); t1.start (); Thread T2 = new Thread (()-> {readLock.lock (); try {System.out.println ("[T2] get read lock."); Thread.sleep (3000);} catch (InterruptedException e) {e.printStackTrace () } finally {System.out.println ("[T2] release read lock."); readLock.unlock ();}}); t2.start ()
The results of the above procedures are as follows:
1.2 read-write mutual exclusion
The simultaneous use of read and write locks is mutually exclusive (that is, it cannot be obtained at the same time), which is called read-write mutual exclusion, as shown in the following code:
/ / create read / write lock final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock (); / / create read lock final ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock (); / / create write lock final ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock (); / / use read lock Thread T1 = new Thread (()-> {readLock.lock (); try {System.out.println ("[T1] get read lock."); Thread.sleep (3000) } catch (InterruptedException e) {e.printStackTrace ();} finally {System.out.println ("[T1] release read lock."); readLock.unlock ();}}); t1.start (); / use write lock Thread T2 = new Thread (()-> {writeLock.lock (); try {System.out.println ("[T2] gets the write lock.") Thread.sleep (3000);} catch (InterruptedException e) {e.printStackTrace ();} finally {System.out.println ("[T2] release write lock."); writeLock.unlock ();}}); t2.start ()
The results of the above procedures are as follows:
1.3 Writing mutual exclusion
Multiple threads using write locks at the same time are also mutually exclusive, which is called write mutex, as shown in the following code:
} finally {System.out.println ("[T2] release write lock."); writeLock.unlock ();}}); t2.start ()
The results of the above procedures are as follows:
two。 Merit analysis
Improve the program execution performance: multiple read locks can be executed at the same time, compared with ordinary locks have to queue for execution in any case, read-write locks improve the execution performance of the program.
Avoid reading temporary data: read and write locks are queued for mutual exclusion, which ensures that read operations do not read half-written temporary data.
3. Applicable scenario
Read-write locks are suitable for business scenarios where there are more reads and less writes, and read-write locks have the greatest advantage.
This is the end of the article on "how to use java read-write locks and what are their advantages". Thank you for reading! I believe you all have a certain understanding of "how to use java read-write locks and what are their advantages". 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.