• 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!

  • 相关阅读:
    隐私协议 Secret Network 宣布使用 Octopus Network 构建的 NEAR-IBC 连接 NEAR 生态
    云服务器 CentOS7 操作系统上安装Jpress (Tomcat 部署项目)
    软考-软件设计师 - 第12章 软件系统分析与设计【附补充常考知识点】
    灵魂一问:一个Java文件的执行全部过程你确定都清楚吗?
    AIGC(生成式AI)试用 9 -- 完整的程序
    Oracle查询用户所有表的语句
    php foreach变量引用的问题
    在项目中使用TS封装 axios,一次封装永久使用
    【计算机网络】虚拟路由冗余(VRRP)协议原理与配置
    CTF 全讲解:[SWPUCTF 2021 新生赛]jicao
  • 原文地址:https://blog.csdn.net/xuan__xia/article/details/134501280