范例1:
package com.contoso;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ConcurrentHashMapExample1 {
/**
* ConcurrentHashMap的并发访问.
*
* ConcurrentHashMap使用的是区间(或称分段)锁,锁定它里面存储的全部键-值对
*
* 区间锁通过几个锁来划分被保护的区域
*
* - 不需要抛ConcurrentModificationException
*
* - ConcurrentHashMap的size() 和 isEmpty() 可能是不正确的, 不要信赖它们
*
* - ConcurrentHashMap支持原子操作, 不需要客户端锁定
*
* - 读取数据可以并发访问ConcurrentHashMap, 它的迭代器具有弱一致性
*
* - 只有少数几个写操作可以同时修改ConcurrentHashMap
*/
public static void useConcurrentHashMap() {
System.out.println("=== ConcurrentHashMap ===");
ExecutorService executor = Executors.newCachedThreadPool();
Random random = new Random();
ConcurrentHashMap map = new ConcurrentHashMap<UUID, Integer>();
// putIfAbsent是原子操作
// 如果key对应的值value不存在就put,且返回null;
// 如果key对应的值value已存在,则返回已存在的值,且value不能为null,否则会报空指针异常。
map.putIfAbsent(UUID.randomUUID(), random.nextInt(50));
// 模拟并发访问 - 插入不重复的键-值对
for (int i = 0; i < 100; i++) {
if (i % 3 == 0) {
// 写操作
executor.execute(() -> {
UUID key = UUID.randomUUID();
Integer value = random.nextInt(100);
System.out.println("Write " + key + " : " + value);
map.putIfAbsent(key, value);
});
} else {
// 读操作
executor.execute(() -> System.err.println("Read " + map.values().toString()));
}
}
executor.shutdown();
}
public static void main(String[] args) {
useConcurrentHashMap();
}
}
run:
=== ConcurrentHashMap ===
Write c93da429-fdd8-4281-a5e2-69834c6af413 : 41
Read [41, 15]
Read [41, 15]
Read [41, 15]
Read [41, 15]
Write b75e309a-e2e3-4683-a04c-3a90b240ce01 : 31
Read [41, 31, 15]
Write b35b76b4-1638-418f-a635-d4e3f2fe64c7 : 19
Read [41, 19, 31, 15]
Write 1742a44d-049b-4d85-9c49-9bf047216d69 : 48
Read [48, 41, 19, 31, 15]
Read [48, 41, 19, 31, 15]
Read [48, 41, 19, 31, 15]
Read [48, 41, 19, 31, 15]
Read [48, 41, 19, 31, 15]
Read [48, 41, 19, 31, 15]
Write f90e60f0-dddb-4c09-ab0f-c43a1eb98a6e : 36
Read [48, 41, 19, 31, 15, 36]
Read [48, 41, 19, 31, 15, 36]
Read [48, 41, 19, 31, 15, 36]
Read [48, 41, 19, 31, 15, 36]
Write 2d6a2f8b-2404-4847-8bfb-cb5a7cd0b4ab : 24
Read [48, 41, 19, 24, 31, 15, 36]
Read [48, 41, 19, 24, 31, 15, 36]
Write 466ddf0c-7860-410c-8ca1-811a66ba4647 : 10
Read [48, 41, 19, 24, 31, 15, 10, 36]
Read [48, 41, 19, 24, 31, 15, 10, 36]
Read [48, 41, 19, 24, 31, 15, 10, 36]
Read [48, 41, 19, 24, 31, 15, 10, 36]
Write 29008ef0-9ccd-4a1d-9095-a06bdbd88f47 : 10
Read [48, 10, 41, 19, 24, 31, 15, 10, 36]
Read [48, 10, 41, 19, 24, 31, 15, 10, 36]
Read [48, 10, 41, 19, 24, 31, 15, 10, 36]
Read [48, 10, 41, 19, 24, 31, 15, 10, 36]
Read [48, 10, 41, 19, 24, 31, 15, 10, 36]
Read [48, 10, 41, 19, 24, 31, 15, 10, 36]
Write 3519230b-2481-463d-a8ff-af5ed544e225 : 30
Read [48, 10, 41, 19, 24, 31, 15, 10, 36]
Write 1f63fefa-bdbb-4a2b-a842-aea424044df0 : 45
Read [48, 10, 41, 19, 24, 31, 15, 10, 45, 36, 30]
Read [48, 10, 41, 19, 24, 31, 15, 10, 45, 36, 30]
Write c41082e0-135a-4e27-bbb2-36c8f1e80807 : 13
Write 6eeb2c87-46c8-4d1e-8637-438730c350ef : 46
Read [48, 10, 46, 10, 36, 30, 41, 19, 24, 13, 31, 15, 45]
Read [48, 10, 46, 10, 36, 30, 41, 19, 24, 13, 31, 15, 45]
Read [48, 10, 46, 10, 36, 30, 41, 19, 24, 13, 31, 15, 45]
Write d310bc76-ad74-48f7-b2d3-11c4cf84743b : 18
Read [48, 10, 46, 10, 18, 36, 30, 41, 19, 24, 13, 31, 15, 45]
Read [48, 10, 46, 10, 18, 36, 30, 41, 19, 24, 13, 31, 15, 45]
Read [48, 10, 46, 10, 18, 36, 30, 41, 19, 24, 13, 31, 15, 45]
Write 37cc879d-693f-49f0-8120-94a742a94df8 : 0
Read [48, 10, 46, 0, 10, 18, 36, 30, 41, 19, 24, 13, 31, 15, 45]
Read [48, 10, 46, 0, 10, 18, 36, 30, 41, 19, 24, 13, 31, 15, 45]
Write 898596de-9211-4f5a-bba4-f089eaec5102 : 47
Read [48, 10, 46, 0, 10, 18, 36, 30, 47, 41, 19, 24, 13, 31, 15, 45]
Read [48, 10, 46, 0, 10, 18, 36, 30, 47, 41, 19, 24, 13, 31, 15, 45]
Write 0e8d4fa4-d242-4bda-9be9-f771c14e6dcd : 40
Read [48, 10, 46, 0, 10, 18, 36, 30, 47, 41, 19, 24, 13, 31, 15, 45]
Read [48, 10, 46, 0, 40, 10, 18, 36, 30, 47, 41, 19, 24, 13, 31, 15, 45]
Write efa71463-55d1-4668-8847-6085a9eee869 : 1
Read [48, 10, 46, 0, 40, 10, 18, 36, 30, 47, 41, 19, 24, 13, 31, 15, 45]
Read [48, 10, 46, 0, 40, 10, 18, 36, 30, 47, 41, 19, 24, 13, 1, 31, 15, 45]
Read [48, 10, 46, 0, 40, 10, 18, 36, 30, 47, 41, 19, 24, 13, 1, 31, 15, 45]
Write 420addc9-4e89-4630-b1d6-bc97af487dc6 : 38
Write 621a6d92-e46a-431a-a66e-59173a97efb4 : 34
Read [48, 10, 34, 46, 0, 40, 10, 18, 36, 30, 47, 41, 38, 19, 24, 13, 1, 31, 15, 45]
Write eb3dfcbd-2b1b-453c-9213-df871784321f : 28
Read [48, 10, 34, 46, 0, 40, 10, 18, 36, 30, 47, 41, 38, 19, 24, 13, 1, 31, 15, 45]
Read [48, 10, 34, 46, 0, 28, 40, 10, 18, 36, 30, 47, 41, 38, 19, 24, 13, 1, 31, 15, 45]
Write 7dd73bd6-24b0-46c6-b641-9d1b4ce1de5a : 46
Write 054deb2d-4806-4a3b-a525-b3076c7a1a4e : 22
Read [48, 10, 34, 46, 0, 28, 40, 10, 18, 36, 30, 47, 41, 38, 46, 19, 24, 13, 1, 31, 15, 45]
Write 1e3f6c76-952a-421b-ad0d-8785b2ce4cf8 : 22
Write b221c6cc-a095-483c-b84b-778a40729305 : 14
Read [10, 22, 0, 10, 22, 18, 36, 47, 41, 46, 13, 45, 48, 34, 46, 28, 40, 30, 38, 19, 24, 1, 31, 15]
Write ae961f26-72f5-456e-8af3-75f6d5c605ac : 6
Write 7e143d8d-6449-4bc5-883a-a4637014279e : 5
Read [10, 6, 22, 5, 0, 10, 22, 18, 36, 47, 41, 46, 13, 45, 48, 34, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Write a6c0656c-dfe3-475a-b917-10c085f7f0a1 : 14
Read [10, 6, 22, 5, 0, 10, 22, 18, 36, 47, 41, 46, 13, 45, 48, 34, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Write e0e181b4-9bf8-4527-b24d-529c1daa6724 : 46
Read [10, 6, 22, 5, 0, 10, 22, 18, 36, 47, 41, 46, 13, 45, 48, 34, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Write b2d3447b-6aee-4519-9cd7-6a9e3f64cf90 : 10
Write 762264dc-b6ca-46d9-90f8-f1dc064cd350 : 49
Read [10, 6, 22, 5, 0, 10, 22, 18, 36, 47, 41, 46, 13, 45, 48, 34, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Write cd9f2991-1637-45f4-87b1-d796e58e0162 : 34
Write f8b94329-1d52-4bc8-b83d-3da25580b243 : 26
Write 5575c2e6-96e8-4f1f-b09b-6ab73009e244 : 46
Read [10, 6, 22, 5, 0, 10, 22, 18, 36, 47, 41, 46, 13, 45, 48, 34, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Write aa421a55-bb91-489b-9de7-caf7550d2150 : 31
Read [10, 6, 22, 5, 0, 10, 22, 18, 36, 47, 14, 41, 46, 13, 45, 48, 34, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Read [10, 6, 22, 5, 0, 10, 22, 18, 36, 47, 14, 41, 46, 13, 45, 48, 34, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Read [10, 6, 22, 5, 0, 10, 22, 18, 36, 47, 14, 41, 46, 13, 45, 48, 34, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Read [10, 6, 22, 5, 0, 10, 22, 18, 36, 47, 14, 41, 46, 13, 45, 48, 34, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Read [10, 6, 22, 5, 0, 10, 22, 18, 36, 47, 14, 41, 46, 13, 45, 48, 34, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Read [10, 6, 22, 5, 0, 10, 22, 18, 36, 47, 14, 41, 46, 46, 13, 45, 48, 34, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Read [10, 6, 22, 5, 0, 10, 22, 18, 36, 47, 14, 41, 46, 49, 46, 13, 45, 48, 34, 10, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Read [10, 6, 22, 5, 0, 10, 22, 18, 36, 47, 14, 41, 46, 49, 46, 13, 45, 48, 34, 10, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Read [10, 6, 22, 5, 34, 0, 10, 22, 18, 36, 47, 14, 41, 46, 49, 46, 26, 13, 45, 48, 34, 10, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
Read [10, 6, 46, 22, 5, 34, 0, 10, 22, 18, 36, 47, 14, 41, 46, 49, 46, 26, 13, 45, 48, 34, 10, 46, 28, 40, 30, 14, 38, 19, 24, 1, 31, 15]
BUILD SUCCESSFUL (total time: 1 second)