读写锁(ReentrantReadWriteLock)理解和例子说明

提示:这里我只是聊聊自己的理解,相当于自己记个笔记而已,如果有误,请指正。

原因:多个线程访问同一个资源的时候,应该读锁是共享锁,写锁是独占锁。即:读--读共存,而读--写和写--写不能共存。

例子(一个读和写都共享的例子):

package com.wrb.lock;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:wrb
 * @Date :2020/2/2/22:53
 * @Describtion:
 **/
public class ReadWriteLockDemo2 {

    public static void main(String[] args) {
        MyCache2 myCache2 = new MyCache2();
        for (int i = 1;i<=5;i++){
            final int tmp = i;
            new Thread(()->{
                //写
                myCache2.put(tmp+"", tmp+"");
            },"写"+tmp).start();
        }
        for (int i = 1;i<=5;i++){
            final int tmp = i;
            new Thread(()->{
                //读
                myCache2.get(tmp+"");
            },"读"+tmp).start();
        }
    }
}

class MyCache2{
    //共享的资源类
    Map map = new HashMap<>();
    //写
    public void put(String key,String value){
        System.out.println(Thread.currentThread().getName()+"\t 正在写入...");
        map.put(key,value );
        System.out.println(Thread.currentThread().getName()+"\t 写入完成");
    }
    //读
    public void get(String key){
        System.out.println(Thread.currentThread().getName()+"\t 正在读...");
        System.out.println(Thread.currentThread().getName()+"\t 读完成,值为:"+map.get(key));
    }

}

测试结果:

读写锁(ReentrantReadWriteLock)理解和例子说明_第1张图片

读写锁闪亮登场:

package com.wrb.lock;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Author:wrb
 * @Date :2020/2/2/22:53
 * @Describtion:
 **/
public class ReadWriteLockDemo2 {
    public static void main(String[] args) {
        MyCache2 myCache2 = new MyCache2();
        for (int i = 1;i<=5;i++){
            final int tmp = i;
            new Thread(()->{
                //写
                myCache2.put(tmp+"", tmp+"");
            },"写"+tmp).start();
        }
        for (int i = 1;i<=5;i++){
            final int tmp = i;
            new Thread(()->{
                //读
                myCache2.get(tmp+"");
            },"读"+tmp).start();
        }
    }
}

class MyCache2{
    //共享的资源类
    Map map = new HashMap<>();

    //读写锁(************添加)
    private ReentrantReadWriteLock rwlock = new ReentrantReadWriteLock();
    //写
    public void put(String key,String value){
        //加上写锁
        rwlock.writeLock().lock();
        try {
            System.out.println(Thread.currentThread().getName()+"\t 正在写入...");
            map.put(key,value );
            System.out.println(Thread.currentThread().getName()+"\t 写入完成");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            rwlock.writeLock().unlock();
        }
    }
    //读
    public void get(String key){
        //加上读锁
        rwlock.readLock().lock();
        try {
            System.out.println(Thread.currentThread().getName()+"\t 正在读...");
            System.out.println(Thread.currentThread().getName()+"\t 读完成,值为:"+map.get(key));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            rwlock.readLock().unlock();
        }
    }
}

测试结果:

读写锁(ReentrantReadWriteLock)理解和例子说明_第2张图片

搞定!

努力奋斗,不负韶华!

你可能感兴趣的:(开发技术分享,java,java开发)