Skip to content

并发编程之 StampedLock 高性能读写锁

ReentrantReadWriteLock 的性能已经很好了,但是他底层还是需要进行一系列的 cas 操作去加锁;StampedLock 如果是读锁上锁,是没有这种 cas 操作的,性能比 ReentrantReadWriteLock 更好。

这样的锁也称为乐观读锁;即读获取锁的时候是不加锁,直接返回一个值;然后执行临界区的时候去验证这个值是否有被人修改(写操作加锁)

如果没有被人修改则直接执行临界区的代码;如果被人修改了则需要升级为读写锁(ReentrantReadWriteLock--->readLock);

基本语法

读锁

java
//获取戳 不存在锁
long stamp = lock.tryOptimisticRead();
//验证戳
if(lock.validate(stamp)){
	//成立则执行临界区的代码。此处省略了业务代码
    // ......
}

try {
    ////如果没有返回则表示被人修改了 需要升级成为readLock。锁的升级 也会改戳
    stamp = lock.readLock();
    // 执行临界区的代码
    // ......
} finally {
    lock.unlockRead(stamp);
}

写锁

java
stampWrite = lock.writeLock();
try {
    // 执行临界区的代码
    // ......
} finally {
    lock.unlockWrite(stampWrite);
}

代码示例

java
package com.mengweijin.learning.basic.lock;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.StampedLock;

/**
 * 一个数据容器
 * 不支持重入
 * 不支持条件
 * 读读并发,读写互斥,写写互斥
 * @author mengweijin
 */
@Slf4j
public class StampedLockDemo {

    private int i = 0;
    private long stampWrite = 0L;

    private final StampedLock lock = new StampedLock();

    public static void main(String[] args) {
        StampedLockDemo demo = new StampedLockDemo();
//        doubleReadLock(demo);
//        doubleWriteLock(demo);
        readWriteLock(demo);
    }

    public static void doubleReadLock(StampedLockDemo demo) {
        new Thread(() -> demo.read(), "t1-read").start();
        new Thread(() -> demo.read(), "t2-read").start();
    }

    public static void doubleWriteLock(StampedLockDemo demo) {
        new Thread(() -> demo.write(1), "t1-read").start();
        new Thread(() -> demo.write(2), "t2-read").start();
    }

    public static void readWriteLock(StampedLockDemo demo) {
        new Thread(() -> demo.read(), "t1-read").start();
        new Thread(() -> demo.write(2), "t2-read").start();
    }

    @SneakyThrows
    public int read() {
        //尝试一次乐观读
        long stamp = lock.tryOptimisticRead();
        log.debug("StampedLock 读锁拿到的戳 {}", stamp);
        //1s之后验戳
        TimeUnit.SECONDS.sleep(1);
        if (lock.validate(stamp)) {
            log.debug("StampedLock 验证完毕 stamp={}, i={}", stamp, i);
            return i;
        }
        //验证失败
        log.debug("验证失败 被写线程给改变了 {}", stampWrite);
        try {
            //锁的升级 也会改戳
            stamp = lock.readLock();
            log.debug("升级之后的加锁成功 {}", stamp);
            TimeUnit.SECONDS.sleep(1);
            log.debug("升级读锁完毕 stamp={}, i={}", stamp, i);
            return i;
        } finally {
            log.debug("升级锁解锁 {}", stamp);
            lock.unlockRead(stamp);
        }
    }

    @SneakyThrows
    public void write(int i) {
        //cas 加锁
        stampWrite = lock.writeLock();
        log.debug("写锁加锁成功 {}", stampWrite);
        try {
            TimeUnit.SECONDS.sleep(5);
            this.i = i;
        } finally {
            log.debug("写锁解锁 stamp={}, i={}", stampWrite, i);
            lock.unlockWrite(stampWrite);
        }
    }
}

StampedLock 的性能这么好能否替代 ReentrantReadWriteLock?

不能。

  • StampedLock 不支持锁重入;
  • StampedLock 不支持条件队列;
  • StampedLock 存在一定的并发问题。