Home >Java >javaTutorial >How are read-write locks used in concurrent programming in Java?
Read-write lock is a concurrency control mechanism that allows multiple threads to read shared resources concurrently, but only one thread can write at a time. It is primarily used for applications with read-intensive workloads and sporadic writes. In Java, read-write locks can be implemented using the java.util.concurrent.locks.ReadWriteLock interface, where read locks allow read access and write locks allow write access. For example, in a shared counter, multiple threads can read the counter value concurrently, and the writing thread needs to acquire a write lock to update the counter, ensuring write atomicity and data integrity.
Introduction
Read-write lock is a Concurrency control mechanism allows multiple threads to read shared resources concurrently, but only one thread can write to shared resources at a time. This is useful for applications with read-intensive workloads and occasional writes.
Unlike mutex locks, read-write locks allow multiple readers to access shared resources at the same time, while writers have exclusive access to the resources.
Using read-write locks in Java
java.util.concurrent.locks.ReadWriteLock interface in Java provides read-write locks Function. It has two types of locks:
The following is an example of using a read-write lock:
import java.util.concurrent.locks.ReentrantReadWriteLock; public class SharedResource { private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); private int value; public void read() { lock.readLock().lock(); try { // 读取共享资源 System.out.println("Reading: " + value); } finally { lock.readLock().unlock(); } } public void write(int newValue) { lock.writeLock().lock(); try { // 写入共享资源 value = newValue; System.out.println("Writing: " + value); } finally { lock.writeLock().unlock(); } } }
Practical case
Consider a shared counter where multiple threads read Take the counter value while only one thread updates it. We can use read-write locks to ensure data integrity during concurrent access.
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CounterExample { private final SharedResource counter = new SharedResource(); private final ExecutorService executor = Executors.newFixedThreadPool(10); public void run() { // 创建 10 个读取线程 for (int i = 0; i < 10; i++) { executor.submit(counter::read); } // 模拟写入线程 for (int i = 0; i < 100; i++) { executor.submit(() -> counter.write(i)); } executor.shutdown(); } public static void main(String[] args) { new CounterExample().run(); } }
In this example, multiple reading threads can read the counter value concurrently, and the writing thread acquires the write lock before accessing the counter. This ensures atomicity of write operations and data integrity.
The above is the detailed content of How are read-write locks used in concurrent programming in Java?. For more information, please follow other related articles on the PHP Chinese website!