ReadWriteLock 的简单使用

前文

一文搞懂 Java 线程

谈谈对ReentrantLock的理解

什么是 ReadWriteLock ?

ReadWriteLock 是 java.util.concurrent.locks 包下的一个接口,该接口允许一次读取多个线程,但一次只能写入一个线程

  • 读锁:如果没有线程锁定 ReadWriteLock 进行写入,则多线程可以访问读锁
  • 写锁:如果没有线程正在读或写,那么一个线程可以访问写锁

ReadWriteLock 定义

public interface ReadWriteLock

锁方法

以下是 Lock 类中可用的重要方法列表

方法 描述
public Lock readLock() 返回用于读的锁
public Lock writeLock() 返回用于写的锁

举例(使用读写锁)

以下TestThread程序演示了ReadWriteLock接口的这些方法。这里我们使用readlock()获取读锁定和writeLock()来获取写锁定

package com.java.springtest.testdemo;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author Woo_home
 * @create by 2020/2/2
 */
public class TestThread {

    // 开启公平锁
    private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);

    // 定义消息属性
    private static String message = "";

    // 定义读锁
    static class Reader implements Runnable {
        @Override
        public void run() {
            if (lock.isWriteLocked()) {
                System.out.println("Write Lock Present");
            }
            lock.readLock().lock();
            try {
                Long duration = (long) (Math.random() * 10000);
                System.out.println(Thread.currentThread().getName() + " Time Taken " + (duration / 1000) + " seconds");
                Thread.sleep(duration);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println(Thread.currentThread().getName() + ": " + message);
                lock.readLock().unlock();
            }
        }
    }

    // 定义写锁 WriterA
    static class WriterA implements Runnable {
        @Override
        public void run() {
            lock.writeLock().lock();
            try {
                Long duration = (long) (Math.random() * 10000);
                System.out.println(Thread.currentThread().getName() + " Time Taken " + (duration / 1000) + " second");
                Thread.sleep(duration);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                message = message.concat("a");
                lock.writeLock().unlock();
            }
        }
    }

    // 定义写锁 WriterB
    static class WriterB implements Runnable {
        @Override
        public void run() {
            lock.writeLock().lock();
            try {
                Long duration = (long) (Math.random() * 10000);
                System.out.println(Thread.currentThread().getName() + " Time Taken " + (duration / 1000) + " second");
                Thread.sleep(duration);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                message = message.concat("b");
                lock.writeLock().unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        // 启动线程
        Thread thread1 = new Thread(new WriterA());
        thread1.setName("Writer A");
        Thread thread2 = new Thread(new WriterB());
        thread2.setName("Writer B");
        Thread thread3 = new Thread(new Reader());
        thread3.setName("Reader");
        thread1.start();
        thread2.start();
        thread3.start();
        
        // 线程强制执行
        thread1.join();
        thread2.join();
        thread3.join();
    }
}

输出:
在这里插入图片描述

发布了196 篇原创文章 · 获赞 961 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/Woo_home/article/details/104141855