1、多个线程可以同时持有读锁,只要没有线程持有写锁。
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLockExample {
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private final ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
private final ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();
public void readOperation() {
readLock.lock();
try {
System.out.println(Thread.currentThread().getName() + " acquired read lock");
Thread.sleep(2000); // Simulate read operation
System.out.println(Thread.currentThread().getName() + " is reading");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
readLock.unlock();
System.out.println(Thread.currentThread().getName() + " released read lock");
}
}
public void writeOperation() {
writeLock.lock();
try {
System.out.println(Thread.currentThread().getName() + " acquired write lock");
Thread.sleep(2000); // Simulate write operation
System.out.println(Thread.currentThread().getName() + " is writing");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
writeLock.unlock();
System.out.println(Thread.currentThread().getName() + " released write lock");
}
}
public static void main(String[] args) {
ReadWriteLockExample example = new ReadWriteLockExample();
// Creating multiple threads to perform read operations
Thread t1 = new Thread(example::readOperation, "t1");
Thread t2 = new Thread(example::readOperation, "t2");
Thread t3 = new Thread(example::readOperation, "t3");
// Starting read threads
t1.start();
t2.start();
t3.start();
// Adding a delay to ensure read operations start first
try {
Thread.sleep(500); // Ensure read operations start first
} catch (InterruptedException e) {
e.printStackTrace();
}
// Creating a thread to perform write operation
Thread t4 = new Thread(example::writeOperation, "t4");
// Starting write thread
t4.start();
}
}
运行结果
t1 acquired read lock
t2 acquired read lock
t3 acquired read lock
t1 is reading
t2 is reading
t3 is reading
t1 released read lock
t2 released read lock
t3 released read lock
t4 acquired write lock
t4 is writing
t4 released write lock
2、t1线程持有读锁时,t2线程无法获取写锁。没有线程持有读锁或写锁时,线程才能获取写锁。
以下是一个示例,展示了当一个线程持有读锁时,另一个线程无法获取写锁的情况:
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLockExample {
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private final ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
private final ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();
public void readOperation() {
readLock.lock();
try {
System.out.println(Thread.currentThread().getName() + " acquired read lock");
Thread.sleep(5000); // Simulate read operation
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
readLock.unlock();
System.out.println(Thread.currentThread().getName() + " released read lock");
}
}
public void writeOperation() {
writeLock.lock();
try {
System.out.println(Thread.currentThread().getName() + " acquired write lock");
Thread.sleep(5000); // Simulate write operation
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
writeLock.unlock();
System.out.println(Thread.currentThread().getName() + " released write lock");
}
}
public static void main(String[] args) {
ReadWriteLockExample example = new ReadWriteLockExample();
Thread t1 = new Thread(example::readOperation, "t1");
Thread t2 = new Thread(example::writeOperation, "t2");
t1.start();
try {
Thread.sleep(1000); // Ensure t2 starts first
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
}
}
运行结果
t1 acquired read lock
... (5 seconds later)
t1 released read lock
t2 acquired write lock
... (5 seconds later)
t2 released write lock
3、t1线程持有写锁时,t2线程无法获取读写锁。一旦某个线程持有写锁,其他线程无法同时持有读锁或写锁。