• ReentranReadWriteLock 使用案例


     ReentranReadWriteLock使用案例

    1. /**
    2. * ReentranReadWriteLock 使用案例
    3. * 读线程共享
    4. * 写线程互斥
    5. */
    6. public class ReentrantReadWriteLockExample {
    7. private String news;
    8. private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    9. public String readNews() {
    10. lock.readLock().lock();
    11. try {
    12. // 读线程睡眠1秒, 观察打印时间
    13. Thread.sleep(1000);
    14. System.out.println(Thread.currentThread().getName() + " " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")) + " is reading the news: " + news);
    15. return news;
    16. } catch (InterruptedException e) {
    17. throw new RuntimeException(e);
    18. } finally {
    19. lock.readLock().unlock();
    20. }
    21. }
    22. public void writeNews(String updatedNews) {
    23. lock.writeLock().lock();
    24. try {
    25. // 让写线程睡眠两秒,这样可以观察到写线程阻塞
    26. Thread.sleep(2000);
    27. System.out.println(Thread.currentThread().getName() + " " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")) + " is updating the news to: " + updatedNews);
    28. news = updatedNews;
    29. } catch (InterruptedException e) {
    30. throw new RuntimeException(e);
    31. } finally {
    32. lock.writeLock().unlock();
    33. }
    34. }
    35. public static void main(String[] args) {
    36. ReentrantReadWriteLockExample newsData = new ReentrantReadWriteLockExample();
    37. // 创建多个读线程
    38. for (int i = 0; i < 10; i++) {
    39. new Thread(() -> {
    40. newsData.readNews();
    41. }).start();
    42. }
    43. // 创建多个写线程
    44. for (int i = 0; i < 5; i++) {
    45. new Thread(()->{
    46. newsData.writeNews("Breaking news: JavaAssistant is awesome!");
    47. }).start();
    48. }
    49. }
    50. }

     输出结果

    从结果可以看出,读线程是同时读取了news,而写线程则是一个个的读。

    1. Thread-0 10:11:45 is reading the news: null
    2. Thread-7 10:11:45 is reading the news: null
    3. Thread-6 10:11:45 is reading the news: null
    4. Thread-1 10:11:45 is reading the news: null
    5. Thread-5 10:11:45 is reading the news: null
    6. Thread-3 10:11:45 is reading the news: null
    7. Thread-2 10:11:45 is reading the news: null
    8. Thread-9 10:11:45 is reading the news: null
    9. Thread-4 10:11:45 is reading the news: null
    10. Thread-8 10:11:45 is reading the news: null
    11. Thread-10 10:11:47 is updating the news to: Breaking news: JavaAssistant is awesome!
    12. Thread-11 10:11:49 is updating the news to: Breaking news: JavaAssistant is awesome!
    13. Thread-12 10:11:51 is updating the news to: Breaking news: JavaAssistant is awesome!
    14. Thread-13 10:11:53 is updating the news to: Breaking news: JavaAssistant is awesome!
    15. Thread-14 10:11:55 is updating the news to: Breaking news: JavaAssistant is awesome!

  • 相关阅读:
    【汇编】第一个汇编程序(学习笔记)
    SuperMap 云原生常见问题解决办法-consul启动异常
    从零开始写 Docker(六)---实现 mydocker run -v 支持数据卷挂载
    SQL通用语法与DDL操作
    关于面向对象与面向过程的基本概念
    LeetCode 周赛 352(2023/07/02)一场关于子数组的专题周赛
    2023年PMP考试会用新版教材吗?回复来了!
    【观察者模式】
    莞中集训8.2
    基于 hugging face 预训练模型的实体识别智能标注方案:生成doccano要求json格式
  • 原文地址:https://blog.csdn.net/xuan__xia/article/details/134501280