" + key);">
当前位置:   article > 正文

【JUC并发编程】读写锁:ReadWriteLock

【JUC并发编程】读写锁:ReadWriteLock

一、介绍

在这里插入图片描述

二、代码演示

1. 不使用读写锁

package readwritelock;

import java.util.HashMap;
import java.util.Map;

/**
 * @author swaggyhang
 * @create 2023-07-09 11:16
 */
public class Test01 {
    public static void main(String[] args) {
        MyCache myCache = new MyCache();

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

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

class MyCache {

    private volatile Map<String, Object> map = new HashMap<>();

    // 存入
    public void put(String key, Object value) {
        System.out.println("线程" + Thread.currentThread().getName() + ":写入 => " + key);
        map.put(key, value);
        System.out.println("线程" + Thread.currentThread().getName() + ":写入OK ");
    }

    // 取出
    public void get(String key) {
        System.out.println("线程" + Thread.currentThread().getName() + ":取出 => " + key);
        Object o = map.get(key);
        System.out.println("线程" + Thread.currentThread().getName() + ":取出OK");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

2. 使用读写锁

package readwritelock;

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

/**
 * @author swaggyhang
 * @create 2023-07-09 11:27
 */
public class Test02 {
    public static void main(String[] args) {
        MyCacheLock myCacheLock = new MyCacheLock();

        for (int i = 1; i <= 5; i++) {
            final int temp = i;
            new Thread(() -> {
                myCacheLock.put(temp + "", temp);
            }, String.valueOf(i)).start();
        }

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

class MyCacheLock {

    private volatile Map<String, Object> map = new HashMap<>();

    // 读写锁,更加细粒度的控制
    private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    // 存入,写入的时候,只希望同时只有一个线程写入
    public void put(String key, Object value) {
        readWriteLock.writeLock().lock();
        try {
            System.out.println("线程" + Thread.currentThread().getName() + ":写入 => " + key);
            map.put(key, value);
            System.out.println("线程" + Thread.currentThread().getName() + ":写入OK ");
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }

    // 取出,读取的时候,所有人都可以读取
    public void get(String key) {
        readWriteLock.readLock().lock();
        try {
            System.out.println("线程" + Thread.currentThread().getName() + ":取出 => " + key);
            Object o = map.get(key);
            System.out.println("线程" + Thread.currentThread().getName() + ":取出OK");
        } finally {
            readWriteLock.readLock().unlock();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

三、总结

  • ReadWriteLock读写锁特点
    ① 写锁是独占锁,一次只能被一个线程占有
    ② 读锁是共享锁,多个线程可以同时占有

  • 读-读:可以共存

  • 读-写:不能共存

  • 写-写:不能共存

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/956203
推荐阅读
相关标签
  

闽ICP备14008679号