初识ReentrantReadWriteLock

这段代码,我开始的想法是t1是读锁,t2是写锁,t3是读锁
那么t1,上锁了后,t2的写锁是上不了的,t3的读锁是可以上的
那么假如顺序是t1,t2,t3的启动顺序,那么读出count的值:t1:0,t3:0,t2:5
但是实际情况却不是,为什么呢?
这个就是ReentrantReadWriteLock的精妙所在了
写锁加不上后,那么后面的读锁肯定也加不上了
这是因为在写后面的读,必须要在写之后,否则就会读脏数据
所以我猜测,在获取读锁之前肯定要判断一下是否有写锁被阻塞,如果有写锁被阻塞,那么就不可以读
class Data{
    private Integer count;
    private ReentrantReadWriteLock reentrantReadWriteLock;
    Data(){
        this.count=0;
        this.reentrantReadWriteLock=new ReentrantReadWriteLock();
    }

    public void setCount(int count) {
        System.out.println(Thread.currentThread().getName()+"要加写锁了");
        reentrantReadWriteLock.writeLock().lock();
        System.out.println(Thread.currentThread().getName()+"获取了此资源,正在写数据");
        try{
            Thread.sleep(3000);
            this.count = count;
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println(Thread.currentThread().getName()+"修改了了count的值,为:"+count);
            reentrantReadWriteLock.writeLock().unlock();
        }
    }

    public Integer getCount() {
        System.out.println(Thread.currentThread().getName()+"要加读锁了");
        reentrantReadWriteLock.readLock().lock();
        System.out.println(Thread.currentThread().getName()+"获取了此资源,正在读数据");
        try{
            Thread.sleep(3000);
            return count;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return null;
        }finally {
            System.out.println(Thread.currentThread().getName()+"获取了count的值,为:"+count);
            reentrantReadWriteLock.readLock().unlock();
        }
    }
}

public class ReentrantReadWriteLockTest {
    private static Data data=new Data();
    private static CountDownLatch countDownLatch=new CountDownLatch(3);
    public static void main(String[] args) {
        new Thread(new Runnable() {
            public void run() {
                Integer count=data.getCount();
                countDownLatch.countDown();
            }
        },"t1").start();
        new Thread(new Runnable() {
            public void run() {
                int count=5;
                data.setCount(count);
                countDownLatch.countDown();
            }
        },"t2").start();
        new Thread(new Runnable() {
            public void run() {
                Integer count=data.getCount();
                countDownLatch.countDown();
            }
        },"t3").start();



        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(new Runnable() {
            public void run() {
                Integer count=data.getCount();

            }
        },"t4").start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值