HashMap在JDK1.8中并发操作不会出现死循环,只会出现缺数据。测试如下:
1 package JDKSource; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 import java.util.concurrent.TimeUnit; 6 7 /** 8 * @Auther: wenbochang 9 * @Date: 2018/8/5 11 10 */ 11 public class TestHashMap { 12 /** 13 * NUMBER = 10,表示十个线程执行put方法执行了十次 14 * 也就是map中总共有10 * 10 = 100 条数据 15 */ 16 public static final int NUMBER = 10; 17 public static void main(String[] args) throws InterruptedException { 18 19 Map<String, String> map = new HashMap<>(2); 20 21 for (int i = 0; i < NUMBER; i++) { 22 new Thread(new A(map)).start(); 23 } 24 TimeUnit.SECONDS.sleep(1); 25 int cnt = 0; 26 for (Map.Entry<String, String> mp : map.entrySet()) { 27 cnt++; 28 System.out.println(mp.getKey() + " " + mp.getValue()); 29 } 30 // 观察cnt的值是否等于插入的数量,就可以判断数据是否丢失了 31 System.out.println("cnt = " + cnt); 32 } 33 } 34 35 class A implements Runnable { 36 37 Map<String, String> map; 38 39 public A(Map<String, String> map) { 40 this.map = map; 41 } 42 43 @Override 44 public void run() { 45 for (int i = 0; i < TestHashMap.NUMBER; i++) { 46 map.put(i + " " + Thread.currentThread().getName(), "test"); 47 } 48 } 49 }
1: 10个线程执行10次put方法:
结果如下: 结果太长我给折叠了,主要看 最后的 cnt值为多少
1 1 Thread-0 test 2 1 Thread-1 test 3 9 Thread-1 test 4 2 Thread-8 test 5 9 Thread-0 test 6 2 Thread-7 test 7 2 Thread-6 test 8 2 Thread-5 test 9 2 Thread-4 test 10 2 Thread-3 test 11 2 Thread-2 test 12 2 Thread-1 test 13 9 Thread-9 test 14 9 Thread-8 test 15 9 Thread-7 test 16 9 Thread-6 test 17 9 Thread-5 test 18 9 Thread-4 test 19 9 Thread-3 test 20 9 Thread-2 test 21 2 Thread-9 test 22 0 Thread-2 test 23 3 Thread-9 test 24 3 Thread-8 test 25 3 Thread-3 test 26 3 Thread-2 test 27 3 Thread-1 test 28 3 Thread-0 test 29 3 Thread-7 test 30 3 Thread-6 test 31 3 Thread-5 test 32 3 Thread-4 test 33 7 Thread-8 test 34 7 Thread-9 test 35 7 Thread-6 test 36 7 Thread-7 test 37 7 Thread-4 test 38 7 Thread-5 test 39 7 Thread-2 test 40 7 Thread-3 test 41 7 Thread-0 test 42 7 Thread-1 test 43 4 Thread-0 test 44 4 Thread-1 test 45 4 Thread-2 test 46 4 Thread-3 test 47 4 Thread-4 test 48 4 Thread-5 test 49 4 Thread-6 test 50 4 Thread-7 test 51 4 Thread-8 test 52 4 Thread-9 test 53 1 Thread-4 test 54 1 Thread-5 test 55 1 Thread-2 test 56 1 Thread-3 test 57 1 Thread-8 test 58 1 Thread-9 test 59 1 Thread-6 test 60 1 Thread-7 test 61 5 Thread-3 test 62 5 Thread-2 test 63 5 Thread-5 test 64 5 Thread-4 test 65 5 Thread-1 test 66 5 Thread-0 test 67 5 Thread-7 test 68 5 Thread-6 test 69 5 Thread-9 test 70 5 Thread-8 test 71 6 Thread-6 test 72 6 Thread-5 test 73 6 Thread-8 test 74 6 Thread-7 test 75 6 Thread-9 test 76 6 Thread-0 test 77 6 Thread-2 test 78 6 Thread-1 test 79 6 Thread-4 test 80 6 Thread-3 test 81 0 Thread-8 test 82 0 Thread-7 test 83 0 Thread-9 test 84 0 Thread-4 test 85 0 Thread-3 test 86 0 Thread-6 test 87 0 Thread-5 test 88 8 Thread-2 test 89 8 Thread-1 test 90 8 Thread-0 test 91 8 Thread-6 test 92 8 Thread-5 test 93 8 Thread-4 test 94 8 Thread-3 test 95 8 Thread-9 test 96 8 Thread-8 test 97 8 Thread-7 test 98 cnt = 97