JAVA高并发(JUC)之ReadWriteLock(读写锁)

这次讲解的数JUC的ReadWriteLock(读写锁):
多个线程同时读一个资源类没有任何问题,所以为了满足并发量,读取共享资源应该可以同时进行。 但是,如果有一个线程想去共享资源类,就不应该再有其他线程可以对该资源进行读或写。

  • 总结:读读共存 读写不共存 写写不共存
    接下来先看个案例:
    代码如下:
public class ReadWriteLockDemo {
    public static void main(String[] args) {
        MyCache myCache = new MyCache();
        for (int i = 0; i < 5; i++) {
            final int finali= i;
            new Thread(() -> {
                try {
                    myCache.put(finali+"", finali+"");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, String.valueOf(i)).start();
        }

        for (int i = 0; i < 5; i++) {
            final int finali= i;
            new Thread(() -> {
                try {
                    myCache.get(finali+"");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, String.valueOf(i)).start();
        }
    }
}

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

    public void put(String key, Object value) throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "\t-----写入数据key");
        Thread.sleep(3000);
        map.put(key, value);
        System.out.println(Thread.currentThread().getName() + "\t-----写入数据成功");
    }

    public void get(String key) throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "\t读取数据key");
        Thread.sleep(3000);
        Object result = map.get(key);
        System.out.println(Thread.currentThread().getName() + "\t读取数据成功" + result);
    }
}

运行结果如下:
在这里插入图片描述
我们可以看到的是在1进行写入数据的时候,此时还没有写入成功,就已经对1进行了读取操作,就像我们数据库的原子性一样,这里在还没有对数据进行写入完成就进行了读取的操作,所以读取的为空。
接下来我们看看加入了读写锁的效果:
代码如下 :
这里只需要对MyCache进行修改:

class MyCache {
    private volatile Map<String, Object> map = new HashMap<>();
    private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    public void put(String key, Object value) throws InterruptedException {
        readWriteLock.writeLock().lock();  //上写锁
        try {
            System.out.println(Thread.currentThread().getName() + "\t-----写入数据key");
            Thread.sleep(3000);
            map.put(key, value);
            System.out.println(Thread.currentThread().getName() + "\t-----写入数据成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }
    public void get(String key) throws InterruptedException {
        readWriteLock.readLock().lock();  //上读锁
        try {
            System.out.println(Thread.currentThread().getName() + "\t读取数据key");
            Thread.sleep(3000);
            Object result = map.get(key);
            System.out.println(Thread.currentThread().getName() + "\t读取数据成功" + result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readWriteLock.readLock().unlock();
        }
    }
}

运行结果如下:
在这里插入图片描述
可以看到我们对写保持了一致性,读保证了可并发读,防止了在写的时候进行了读的操作,所以这就是读写锁的作用 。

总结:读读共存 读写不共存 写写不共存

猜你喜欢

转载自blog.csdn.net/Pzzzz_wwy/article/details/106245981