突击并发编程JUC系列演示代码地址:
https://github.com/mtcarpenter/JavaTutorial

本章节将学习 ReentrantReadWriteLock(读写锁),ReadWriteLock 也是 java 5之后引入的,之前提到锁(如MutexReentrantLock)根本都是排他锁,这些锁在同一时刻只容许一个线程进行拜访,而读写锁在同一时刻能够容许多个读线程拜访,然而在写线程拜访时,所有的读线程和其余写线程均被阻塞。读写锁保护了一对锁,一个读锁和一个写锁,通过拆散读锁和写锁,使得并发性相比个别的排他锁有了很大晋升。
读写锁的拜访束缚:

  • 读-读不互斥:读读之间不阻塞
  • 读-写互斥:读梗塞写,写也阻塞读
  • 写-写互斥:写写阻塞

ReadWriteLock

ReentrantReadWriteLockReadWriteLock接口的实现,ReadWriteLock仅定义了获取读锁和写锁的两个办法,即readLock()办法和writeLock()办法。

public interface ReadWriteLock {    Lock readLock();    Lock writeLock();}

除了接口办法之外,ReentrantReadWriteLock 还提供了一些便于外界监控其外部工作状态的办法。

  • int getReadLockCount(): 返回以后读锁被获取的次数。该次数不等于获取读锁的线程数,例如,仅一个线程,他间断获取(重进入)了 n 次读锁,那么占据读锁的线程数是 1 ,但该办法返回 n。
  • int getReadHoldCount():返回以后线程获取读锁的次数。该办法在 Java 6 中退出到 ReentrantReadWriteLock 中,应用 ThreadLocal 保留以后线程获取次数,这也使得 Java 6 的实现变得更加简单
  • boolean isWriteLocked() : 判断写锁是否被获取
  • int getWriteHoldCount(): 返回以后写锁被获取的次数

ReentrantReadWriteLock

通过 ReentrantReadWriteLock 实现一个简略的缓存,代码示例如下:

public class LockExample3 {    private static final Map<String, Object> map = new HashMap<>();    private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();    private static final Lock readLock = lock.readLock();    private static final Lock writeLock = lock.writeLock();    /**     * 向 map 存入数据     *     * @param key     * @param value     * @return     */    public static Object put(String key, Object value) {        writeLock.lock();        try {            return map.put(key, value);        } finally {            writeLock.unlock();        }    }    /**     * 获取单个键值的值     *     * @param key     * @return     */    public static Object get(String key) {        readLock.lock();        try {            return map.get(key);        } finally {            readLock.unlock();        }    }    /**     * 获取 map 的键值     *     * @return     */    public static Set<String> getAllKeys() {        readLock.lock();        try {            return map.keySet();        } finally {            readLock.unlock();        }    }    /**     * 革除 map 所有的数据     */    public static void clear() {        writeLock.lock();        try {            map.clear();        } finally {            writeLock.unlock();        }    }}

测试代码

public class LockExample3Test {    // 申请总数    public static int requestTotal = 10;    public static void main(String[] args) throws Exception {        ExecutorService executorService = Executors.newCachedThreadPool();        final CountDownLatch countDownLatch = new CountDownLatch(requestTotal);        for (int i = 0; i < requestTotal; i++) {            final String temp = String.valueOf(i);            executorService.execute(() -> {                try {                    add(temp);                } catch (Exception e) {                }                countDownLatch.countDown();            });        }        // 期待所有的线程运行实现        countDownLatch.await();        // 多线程获取 key        for (int i = 0; i < requestTotal; i++) {            final String temp = String.valueOf(i);            executorService.execute(() -> {                try {                    get(temp);                } catch (Exception e) {                }            });        }        executorService.shutdown();        TimeUnit.SECONDS.sleep(1);        // 获取所有的keys        System.out.println("获取所有的键值\t" + LockExample3.getAllKeys());        //  革除所有的 keys        LockExample3.clear();        // 再次获取所有的keys 发现已被清空        System.out.println("获取所有的键值\t" + LockExample3.getAllKeys());    }    private static void add(String i) {        LockExample3.put(i, Thread.currentThread().getName());    }    private static void get(String i) {        System.out.println(i + "\t" + LockExample3.get(i));    }}

运行后果如下:

0    pool-1-thread-11    pool-1-thread-22    pool-1-thread-34    pool-1-thread-53    pool-1-thread-48    pool-1-thread-97    pool-1-thread-86    pool-1-thread-75    pool-1-thread-69    pool-1-thread-10获取所有的键值    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]获取所有的键值    []

欢送关注公众号 山间木匠 , 我是小春哥,从事 Java 后端开发,会一点前端、通过继续输入系列技术文章以文会友,如果本文能为您提供帮忙,欢送大家关注、点赞、分享反对,_咱们下期再见!_