-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrentHashMapTest.java
More file actions
47 lines (41 loc) · 1.43 KB
/
Copy pathConcurrentHashMapTest.java
File metadata and controls
47 lines (41 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapTest {
public static void main(String[] args) {
ConcurrentHashMapTest concurrentHashMapTest = new ConcurrentHashMapTest();
concurrentHashMapTest.b();
System.out.println("OK");
}
void a(){
ConcurrentHashMap<String,String> concurrentHashMap=new ConcurrentHashMap<>();
HashMap<String,String> hashMap=new HashMap<>();
hashMap.put("a","f");
long start=System.nanoTime();
for (int i = 0; i < 20; i++) {
new Thread(()->{
for (int j = 0; j < 100; j++) {
concurrentHashMap.put(Thread.currentThread().getName()+"-"+j,""+j);
}
},"name"+i).start();
}
System.out.println(System.nanoTime()-start);
}
void test(){
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>(16);
map.computeIfAbsent(
"AaAa",
key -> map.computeIfAbsent("BBBB", key2 -> 42)
);
}
void b(){
//todo 死锁深度分析
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
System.out.println("start.");
map.computeIfAbsent("t",
(String t) -> {
map.put("t", "abc");
return "123";
});
System.out.println("fin.");
}
}