How has ReadWriteLock changed in recent Java versions?

ReadWriteLock, Java concurrency, Java 8, Java 9, Java 11, Java 17, thread safety, performance optimization
Explore the enhancements made to ReadWriteLock in recent Java versions, including performance improvements and additional features that optimize thread safety and concurrency management.

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockExample {
    private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
    private String data = "Initial Data";

    public String readData() {
        rwLock.readLock().lock();
        try {
            return data;
        } finally {
            rwLock.readLock().unlock();
        }
    }

    public void writeData(String newData) {
        rwLock.writeLock().lock();
        try {
            this.data = newData;
        } finally {
            rwLock.writeLock().unlock();
        }
    }

    public static void main(String[] args) {
        ReadWriteLockExample example = new ReadWriteLockExample();

        // Simulating multiple readers and a writer
        Thread reader1 = new Thread(() -> System.out.println(example.readData()));
        Thread reader2 = new Thread(() -> System.out.println(example.readData()));
        Thread writer = new Thread(() -> example.writeData("Updated Data"));

        reader1.start();
        reader2.start();
        writer.start();
    }
}
    

ReadWriteLock Java concurrency Java 8 Java 9 Java 11 Java 17 thread safety performance optimization