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

简介

写一个简单的缓存读写锁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;
    }


}

Java可重入读写锁ReentrantReadWriteLock之实战缓存_第1张图片

正例

加了读写锁后程序执行结果
Java可重入读写锁ReentrantReadWriteLock之实战缓存_第2张图片

最终代码

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;
    }


}

你可能感兴趣的:(Java,多线程,JUC,多线程,java)