Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the concept of java read-write lock

Shulou Source: shulou.com Published: 2022-05-31 10:53:41 09月23日 Update

This article mainly introduces the concept of java read-write lock is what the relevant knowledge, detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that everyone reading this java read-write lock concept is what the article will have some harvest, let's take a look at it.

Read-write locks divide access to a resource (such as a file) into two locks, one read-write lock.

Because of read-write locks, read and write operations between multiple threads will not conflict.

ReadWriteLock is a read-write lock, it is an interface, RentrantReadWriteLock implements this interface.

examples

public class CacheDemo { private Map cache = new HashMap(); private ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); public static void main(String[] args) { } public Object getData(String key) { Object value = null; //First open the read lock and fetch from the cache readWriteLock.readLock().lock(); try { value = cache.get(key); //If no read lock is released in cache, unwrite lock if (value == null) { //corresponds to queryDB() readWriteLock.readLock().unlock(); //Read lock must be unlocked before write lock can be acquired readWriteLock.writeLock().lock(); try { //corresponds to queryDB() value = queryDB(); } finally { //release write lock readWriteLock.writeLock().unlock(); } //and then read the lock again readWriteLock.readLock().lock(); } } finally { //finally release read lock readWriteLock.readLock().unlock(); } return value; } public Object queryDB() { return "aaaa"; }} About "Java read-write lock concept is what" the content of this article is introduced here, thank you for reading! I believe that everyone has a certain understanding of the "java read-write lock concept" knowledge, if you still want to learn more knowledge, welcome to pay attention to the industry information channel.

Tags: Concept knowledge content interface article cache two between value multiple instance easy to operate file article easy to understand more see thread industry resource Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Shulou Information Redmi MySQL