Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize java read-write Lock ReadWriteLock

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to realize java read-write lock ReadWriteLock". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to implement java read-write lock ReadWriteLock".

What is a read-write lock?

Read-write lock is divided into two cases, one is read-time lock, the other is write-time lock, which allows multiple threads to read shared variables at the same time, but only one thread is allowed to write shared variables, which also blocks the read operation when writing shared variables. In this way, when reading, it will not be mutually exclusive and improve the efficiency of reading.

What is a reentrant lock?

Reentrant lock means that if your outer function has acquired the lock in the same thread, then your inner function can also acquire the lock, that is, you can get the lock you have acquired when you re-enter the synchronous code block through a thread, and vice versa. Take a look at a simple example

It is okay to use the above code in this way, and calls such as non-reentrant locks are not allowed.

Let's take a look at the example of ReentrantReadWriteLock in Javadoc, which is mainly about dealing with an example of getting a cached data

You can see that the use is simple, compared to ReentrantLock, there are more roles to distinguish one is a read lock and the other is a write lock.

Upgrade and downgrade of read-write lock

The above code mentions

Must release read lock before acquiring write lock (the read lock must be released before acquiring the write lock)

In other words, the read-write lock does not allow the upgrade of the lock and cannot be upgraded directly from the read lock to the write lock. If the read lock has not been released, acquiring the write lock at this time will cause the write lock to wait forever, eventually causing the relevant threads to block, GG. Remember not to use it this way.

But the degradation of the lock is allowed.

Downgrade by acquiring read lock before releasing write lock (degraded by acquiring a read lock before releasing the write lock)

That is to say, the read lock can be acquired before the write lock is released to achieve the lock degradation!

Another difference between read-write locks is that write locks support conditional variables, that is, newCondition.

The read lock does not support condition variables. If the read lock is called newCondition, UnsupportedOperationException will be thrown.

Read-write locks implement the java.util.concurrent.locks.Lock interface, so tryLock (), lockInterruptibly (), and other methods are supported.

And it also supports fair lock and unfair lock mode, and the bottom layer is also based on AbstractQueuedSynchronizer.

At this point, I believe you have a deeper understanding of "how to implement java read-write lock ReadWriteLock". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report