关键词搜索

源码搜索 ×
×

Java Map取值累加的线程安全问题

发布2019-04-23浏览1646次

详情内容

昨天在开发者头条上面看的一篇文章针对Map相关的线程安全讲解说的很好,今天根据思路还原了场景(隔壁老王半夜为何尖叫?这例子说的有点让老王很忙)。

Java代码:

  1. package com.boonya.concurrent;
  2. import java.util.HashMap;
  3. import java.util.Hashtable;
  4. import java.util.concurrent.ConcurrentHashMap;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.atomic.AtomicInteger;
  8. /**
  9. * @author PJL
  10. *
  11. * @note 功能描述:Add值的多线程安全问题--最优解方式是ConcurrentHashMap+Atomic*级别的原子操作
  12. * @package com.boonya.concurrent
  13. * @filename AddConcurrent.java
  14. * @date 2019年4月23日 下午1:36:42
  15. */
  16. public class AddConcurrent {
  17. /**
  18. * HashMap非线程安全
  19. * @throws InterruptedException
  20. */
  21. public static void test0() throws InterruptedException{
  22. HashMap<String, Integer> map = new HashMap<String,Integer>();
  23. Integer integer = new Integer(1);
  24. map.put("key", integer);
  25. ExecutorService executorService = Executors.newFixedThreadPool(100);
  26. for (int i = 0; i < 1000; i++) {
  27. executorService.execute(new Runnable() {
  28. @Override
  29. public void run() {
  30. map.put("key", map.get("key").intValue()+1) ;
  31. }
  32. });
  33. }
  34. Thread.sleep(3000); //模拟等待执行结束
  35. System.out.println("test0()------" + map.get("key") + "------");
  36. executorService.shutdown();
  37. }
  38. /**
  39. * 严格线程安全的同步非原子操作--非线程安全
  40. * @throws InterruptedException
  41. */
  42. public static void test1() throws InterruptedException{
  43. Hashtable<String, Integer> map = new Hashtable<String,Integer>();
  44. Integer integer = new Integer(1);
  45. map.put("key", integer);
  46. ExecutorService executorService = Executors.newFixedThreadPool(100);
  47. for (int i = 0; i < 1000; i++) {
  48. executorService.execute(new Runnable() {
  49. @Override
  50. public void run() {
  51. map.put("key", map.get("key").intValue()+1) ;
  52. }
  53. });
  54. }
  55. Thread.sleep(3000); //模拟等待执行结束
  56. System.out.println("test1()------" + map.get("key") + "------");
  57. executorService.shutdown();
  58. }
  59. /**
  60. * 线程安全的非原子操作--非线程安全
  61. * @throws InterruptedException
  62. */
  63. public static void test2() throws InterruptedException{
  64. ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<String,Integer>();
  65. Integer integer = new Integer(1);
  66. map.put("key", integer);
  67. ExecutorService executorService = Executors.newFixedThreadPool(100);
  68. for (int i = 0; i < 1000; i++) {
  69. executorService.execute(new Runnable() {
  70. @Override
  71. public void run() {
  72. map.put("key", map.get("key").intValue()+1) ;
  73. }
  74. });
  75. }
  76. Thread.sleep(3000); //模拟等待执行结束
  77. System.out.println("test2()------" + map.get("key") + "------");
  78. executorService.shutdown();
  79. }
  80. /**
  81. * 线程安全的原子操作--线程安全
  82. * @throws InterruptedException
  83. */
  84. public static void test3() throws InterruptedException{
  85. ConcurrentHashMap<String, AtomicInteger> map = new ConcurrentHashMap<String,AtomicInteger>();
  86. AtomicInteger integer = new AtomicInteger(1);
  87. map.put("key", integer);
  88. ExecutorService executorService = Executors.newFixedThreadPool(100);
  89. for (int i = 0; i < 1000; i++) {
  90. executorService.execute(new Runnable() {
  91. @Override
  92. public void run() {
  93. map.get("key").incrementAndGet();
  94. }
  95. });
  96. }
  97. Thread.sleep(3000); //模拟等待执行结束
  98. System.out.println("test3()------" + map.get("key") + "------");
  99. executorService.shutdown();
  100. }
  101. public static void main(String[] args) throws InterruptedException {
  102. AddConcurrent.test0();
  103. AddConcurrent.test1();
  104. AddConcurrent.test2();
  105. AddConcurrent.test3();
  106. }
  107. }

输出结果:

  1. test0()------998------
  2. test1()------998------
  3. test2()------413------
  4. test3()------1001------

这个核心思想就是线程安全的原子操作一定是线程安全的。

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载