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

  • 相关阅读:
    Mysql 约束,基本查询,复合查询与函数
    【PyCharm Community Edition】:打印日志保存成文档
    那些有趣好玩强大的Python库
    动环监控设备维护与故障处理,动环监控系统调试
    共享存储知识
    苍穹外卖
    企业媒体信息发布系统,一键发布省时省力
    使用DBSyncer实现增量Mysql到Mysql的数据同步_DBSyncer1.2.4版本---数据同步之DBSyncer工作笔记006
    nginx rewrite(重定向)
    盛世古董乱世金-数据库稳定到底好不好?
  • 原文地址:https://blog.csdn.net/xuan__xia/article/details/134501280