【Java 并发编程】读写锁 ReentrantReadWriteLock & StampLock 详解
发布人:shili8
发布时间:2025-02-16 18:29
阅读次数:0
**Java 并发编程之读写锁**
在 Java 中,读写锁(ReentrantReadWriteLock)是一种特殊的锁,它允许多个线程同时读取共享资源,而只允许一个线程进行写入操作。这种机制可以显著提高程序的并发性和性能。
**什么是读写锁?**
读写锁是一种分离读写的锁,允许多个线程同时读取共享资源,而只允许一个线程进行写入操作。这意味着,当有多个线程尝试读取共享资源时,读写锁会将它们都允许进入临界区,而当有线程尝试写入共享资源时,读写锁会阻止其他线程进入临界区。
**ReentrantReadWriteLock**
Java 中的 `ReentrantReadWriteLock` 是一种可重入的读写锁,它支持多个线程同时读取共享资源,而只允许一个线程进行写入操作。这种锁可以被多次获得,直到它被释放为止。
**使用 ReentrantReadWriteLock**
要使用 `ReentrantReadWriteLock`,你需要创建一个 `ReentrantReadWriteLock` 对象,然后获取对应的读取锁或写入锁。
javaimport java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLockExample {
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private final int value =0;
public void read() {
lock.readLock().lock();
try {
System.out.println("Reading: " + value);
Thread.sleep(100); // Simulate some work } catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.readLock().unlock();
}
}
public void write(int newValue) {
lock.writeLock().lock();
try {
System.out.println("Writing: " + newValue);
value = newValue;
Thread.sleep(100); // Simulate some work } catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.writeLock().unlock();
}
}
public static void main(String[] args) {
ReadWriteLockExample example = new ReadWriteLockExample();
// Create and start two threads that read the value Thread thread1 = new Thread(() -> example.read());
Thread thread2 = new Thread(() -> example.read());
// Start both threads thread1.start();
thread2.start();
// Wait for both threads to finish try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// Write a new value and print the result example.write(10);
System.out.println("Final value: " + example.value);
}
}
在这个例子中,我们创建了一个 `ReentrantReadWriteLock` 对象,并使用它来保护共享资源。我们定义了两个方法:`read()` 和 `write(int newValue)`,它们分别获取读取锁和写入锁,然后执行相应的操作。
**StampLock**
`StampLock` 是一种特殊的读写锁,它允许多个线程同时读取共享资源,而只允许一个线程进行写入操作。这种锁使用了一个"时间戳"机制来实现。
**使用 StampLock**
要使用 `StampLock`,你需要创建一个 `StampLock` 对象,然后获取对应的读取锁或写入锁。
javaimport java.util.concurrent.locks.StampLock;
public class StampLockExample {
private final StampLock lock = new StampLock();
public void read() {
long stamp = lock.readLock();
try {
System.out.println("Reading: " + stamp);
Thread.sleep(100); // Simulate some work } catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.unlock(stamp);
}
}
public void write() {
long stamp = lock.writeLock();
try {
System.out.println("Writing: " + stamp);
Thread.sleep(100); // Simulate some work } catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.unlock(stamp);
}
}
public static void main(String[] args) {
StampLockExample example = new StampLockExample();
// Create and start two threads that read the value Thread thread1 = new Thread(() -> example.read());
Thread thread2 = new Thread(() -> example.read());
// Start both threads thread1.start();
thread2.start();
// Wait for both threads to finish try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// Write a new value and print the result example.write();
}
}
在这个例子中,我们创建了一个 `StampLock` 对象,并使用它来保护共享资源。我们定义了两个方法:`read()` 和 `write(),它们分别获取读取锁和写入锁,然后执行相应的操作。
**总结**
本文介绍了 Java 中的读写锁(ReentrantReadWriteLock)和 StampLock 的基本概念、使用方法以及示例代码。通过阅读本文,你应该能够理解这些锁的工作原理并在实际项目中应用它们。

