Java可重入读写锁ReentrantReadWriteLock之实战缓存

16 篇文章 0 订阅
8 篇文章 0 订阅

简介

写一个简单的缓存读写锁demo, 基本操作set和get, set是写锁,get是读锁.
读写锁的特点,写锁是独占,读锁是共享

反例

没加读写锁前的程序执行

public class ReadWriteLockDemo {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            final int temp = i;
            new Thread(()->{
                MyCache.set(temp+"",temp+"");
            },i+"").start();
        }

        for (int i = 0; i < 5; i++) {
            final int temp = i;
            new Thread(()->{
                MyCache.get(temp+"");
            },i+"").start();
        }
    }
}

/**
 * 缓存类 读写操作
 */
class MyCache{
    private static volatile Map map = new HashMap();

    public static void set(String key,Object value){
        System.out.println("线程"+Thread.currentThread().getName()+"写入中......");
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        map.put(key,value);
        System.out.println("线程"+Thread.currentThread().getName()+"写入完成");
    }


    public static Object get(String key){
        System.out.println("线程"+Thread.currentThread().getName()+"读取中...");
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Object result = map.get(key);
        System.out.println("线程"+Thread.currentThread().getName()+"读取完成"+result);
        return result;
    }


}

在这里插入图片描述

正例

加了读写锁后程序执行结果
在这里插入图片描述

最终代码

public class ReadWriteLockDemo {
    public static void main(String[] args) {
        MyCache myCache = new MyCache();
        for (int i = 1; i <= 5; i++) {
            final int temp = i;
            new Thread(()->{
                myCache.set(temp+"",temp+"");
            },i+"").start();
        }

        for (int i = 1; i <= 5; i++) {
            final int temp = i;
            new Thread(()->{
                myCache.get(temp+"");
            },i+"").start();
        }
    }
}

/**
 * 缓存类 读写操作
 */
class MyCache{
    private static volatile Map map = new HashMap();
    private static ReadWriteLock rwLock = new ReentrantReadWriteLock();

    public  void set(String key,Object value){

        rwLock.writeLock().lock();
        try {
            System.out.println("线程"+Thread.currentThread().getName()+"写入中......");
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            map.put(key,value);
            System.out.println("线程"+Thread.currentThread().getName()+"写入完成");
        } finally {
            rwLock.writeLock().unlock();
        }
    }


    public  Object get(String key){
        rwLock.readLock().lock();
        try {
            System.out.println("线程"+Thread.currentThread().getName()+"读取中...");
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Object result = map.get(key);
            System.out.println("线程"+Thread.currentThread().getName()+"读取完成"+result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rwLock.readLock().unlock();
        }
        return null;
    }


}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值