ReentrantReadWriteLock 读写锁特点

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线程无法获取读写锁。一旦某个线程持有写锁,其他线程无法同时持有读锁或写锁。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值