-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashMapTestDemo.java
79 lines (73 loc) · 2.24 KB
/
HashMapTestDemo.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.sealde.thread.container;
import com.sealde.thread.tool.SleepTool;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Author: sealde
* @Date: 2019/10/7 下午5:52
*/
public class HashMapTestDemo {
private static Map<String, String> map = new HashMap<>();
// private static Map<String, String> map = new ConcurrentHashMap<>();
// 测试多个线程同时 put 的情况
private static void testPut() throws InterruptedException {
// 线程1 => t1
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 999; i++) {
map.put("thread1_key" + i, "thread1_value" + i);
}
}
});
// 线程2 => t2
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 999; i++) {
map.put("thread2_key" + i, "thread2_value" + i);
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(map.size());
}
// 测试多个线程同时 put 的时候,get 的情况
private static void testPutGet() {
map.put("test get", "ok");
System.out.println(map.get("test get"));
SleepTool.sleep(1000);
// 线程1 => get
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
String get = map.get("test get");
if (get == null) {
System.out.println("null");
break;
}
}
}
});
// 线程2 => put
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
map.put("thread2_key" + i, "thread2_value" + i);
}
}
});
t1.start();
t2.start();
}
public static void main(String[] args) throws InterruptedException {
testPut();
// testPutGet();
}
}