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

一、介绍

【JUC并发编程】读写锁:ReadWriteLock_第1张图片

二、代码演示

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

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

三、总结

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

  • 读-读:可以共存

  • 读-写:不能共存

  • 写-写:不能共存

你可能感兴趣的:(JUC并发编程,java)