日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

        歡迎訪問 生活随笔!

        生活随笔

        當前位置: 首頁 >

        Java并发_volatile实现可见性但不保证原子性

        發布時間:2025/7/14 46 豆豆
        生活随笔 收集整理的這篇文章主要介紹了 Java并发_volatile实现可见性但不保证原子性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
        讀后感
      1. 介紹了volatile實現可見性的基本原理
      2. 介紹了volatile不能實現原子性的示例,volatile復合操作不能實現原子性,讀取值后在自增前改值可能被其它線程讀取并修改,自增后刷新值可能會覆蓋其它線程修改后的值
      3. 介紹了實現原子性的三種方法及示例
      4. synchronized ?修飾對象
      5. ReentrantLock 使用lock()、unlock()加鎖解鎖,比synchronized功能更多,JDK6后性能和synchronized差不多
      6. AtomicInteger ?使用樂觀鎖


      7. ??volatile關鍵字:

        • 能夠保證volatile變量的可見性
        • 不能保證volatile變量復合操作的原子性

        ??volatile如何實現內存可見性:

        ???????? 深入來說:通過加入內存屏障和禁止重排序優化來實現的。

        • 對volatile變量執行寫操作時,會在寫操作后加入一條store屏障指令
        • 對volatile變量執行讀操作時,會在讀操作前加入一條load屏障指令

        ???????? 通俗地講:volatile變量在每次被線程訪問時,都強迫從主內存中重讀該變量的值,而當該變量發生變化時,又會強迫線程將最新的值刷新到主內存。這樣任何時刻,不同的線程總能看到該變量的最新值。

        ?

        ?????????線程寫volatile變量的過程:

        • 改變線程工作內存中volatile變量副本的值
        • 將改變后的副本的值從工作內存刷新到主內存

        ?????????線程讀volatile變量的過程:

        • 從主內存中讀取volatile變量的最新值到線程的工作內存中
        • 從工作內存中讀取volatile變量的副本

        ?????????volatile不能保證volatile變量復合操作的原子性:

        Java代碼??
      8. private?int?number?=?0;??
      9. number++;?//不是原子操作??
      10. ???????? 它分為三步:
        ???????? 讀取number的值
        ???????? 將number的值加1
        ???????? 寫入最新的number的值

        ?

        ??????????保證number自增操作的原子性:

        • 使用synchronized關鍵字
        • 使用ReentrantLock
        • 使用AtomicInteger

        ??????????使用synchronized關鍵字

        Java代碼??
      11. import?java.util.concurrent.ExecutorService;??
      12. import?java.util.concurrent.Executors;??
      13. ??
      14. /**?
      15. ?*?@author?InJavaWeTrust?
      16. ?*/??
      17. public?class?TestSyn?implements?Runnable?{??
      18. ??
      19. ????private?int?number?=?0;??
      20. ??
      21. ????public?int?getNumber()?{??
      22. ????????return?this.number;??
      23. ????}??
      24. ??
      25. ????public?void?run()?{??
      26. ????????increase();??
      27. ????}??
      28. ??
      29. ????public?void?increase()?{??
      30. ????????synchronized?(this)?{??
      31. ????????????this.number++;??
      32. ????????}??
      33. ????}??
      34. ??
      35. ????public?static?void?main(String[]?args)?{??
      36. ????????ExecutorService?exec?=?Executors.newFixedThreadPool(1000);??
      37. ????????TestSyn?syn?=?new?TestSyn();??
      38. ????????for?(int?i?=?0;?i?<?1000;?i++)?{??
      39. ????????????exec.submit(syn);??
      40. ????????}??
      41. ????????System.out.println("number?:?"?+?syn.getNumber());??
      42. ????????exec.shutdown();??
      43. ????}??
      44. }??
      45. ?

        ??????????使用ReentrantLock

        Java代碼??
      46. import?java.util.concurrent.ExecutorService;??
      47. import?java.util.concurrent.Executors;??
      48. import?java.util.concurrent.locks.Lock;??
      49. import?java.util.concurrent.locks.ReentrantLock;??
      50. /**?
      51. ?*?@author?InJavaWeTrust?
      52. ?*/??
      53. public?class?TestRee?implements?Runnable?{??
      54. ??
      55. ????private?Lock?lock?=?new?ReentrantLock();??
      56. ????private?int?number?=?0;??
      57. ??
      58. ????public?int?getNumber()?{??
      59. ????????return?this.number;??
      60. ????}??
      61. ??
      62. ????public?void?run()?{??
      63. ????????increase();??
      64. ????}??
      65. ??
      66. ????public?void?increase()?{??
      67. ????????lock.lock();??
      68. ????????try?{??
      69. ????????????this.number++;??
      70. ????????}?finally?{??
      71. ????????????lock.unlock();??
      72. ????????}??
      73. ????}??
      74. ??
      75. ????public?static?void?main(String[]?args)?{??
      76. ????????TestRee?ree?=?new?TestRee();??
      77. ????????ExecutorService?exec?=?Executors.newFixedThreadPool(1000);??
      78. ????????for?(int?i?=?0;?i?<?1000;?i++)?{??
      79. ????????????exec.submit(ree);??
      80. ????????}??
      81. ????????System.out.println("number?:?"?+?ree.getNumber());??
      82. ????????exec.shutdown();??
      83. ????}??
      84. }??
      85. ?

        ??????????使用AtomicInteger

        Java代碼??
      86. import?java.util.concurrent.ExecutorService;??
      87. import?java.util.concurrent.Executors;??
      88. import?java.util.concurrent.atomic.AtomicInteger;??
      89. ??
      90. /**?
      91. ?*?@author?InJavaWeTrust?
      92. ?*/??
      93. public?class?TestAtomic?implements?Runnable?{??
      94. ??
      95. ????private?static?AtomicInteger?number?=?new?AtomicInteger(0);??
      96. ??
      97. ????public?void?run()?{??
      98. ????????increase();??
      99. ????}??
      100. ??
      101. ????public?void?increase()?{??
      102. ????????number.getAndAdd(1);??
      103. ????}??
      104. ??
      105. ????public?static?void?main(String[]?args)?{??
      106. ????????TestAtomic?ato?=?new?TestAtomic();??
      107. ????????ExecutorService?exec?=?Executors.newFixedThreadPool(1000);??
      108. ????????for?(int?i?=?0;?i?<?1000;?i++)?{??
      109. ????????????exec.submit(ato);??
      110. ????????}??
      111. ????????System.out.println("number?:?"?+?number.get());??
      112. ????????exec.shutdown();??
      113. ????}??
      114. } ?


      115. 來自為知筆記(Wiz)

        《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

        總結

        以上是生活随笔為你收集整理的Java并发_volatile实现可见性但不保证原子性的全部內容,希望文章能夠幫你解決所遇到的問題。

        如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。