-
Notifications
You must be signed in to change notification settings - Fork 343
Expand file tree
/
Copy pathHashMapHangDemo.java
More file actions
48 lines (40 loc) 路 1.5 KB
/
Copy pathHashMapHangDemo.java
File metadata and controls
48 lines (40 loc) 路 1.5 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
48
package fucking.concurrency.demo;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* @author Jerry Lee (oldratlee at gmail dot com)
* @see <a href="http://coolshell.cn/articles/9606.html">Infinite loop of Java HashMap</a>
* by <a href="http://github.com/haoel">@haoel</a>
*/
public class HashMapHangDemo {
private static final Map<Integer, Object> holder = new HashMap<>();
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
holder.put(i, null);
}
// start 2 tasks
new Thread(new ConcurrencyTask()).start();
new Thread(new ConcurrencyTask()).start();
System.out.println("Start the get loop in main!");
for (int i = 0; ; ++i) {
for (int key = 0; key < 10_000; ++key) {
holder.get(key);
// If the HashMap hangs, the following output will not appear again.
// On my dev machine, this problem is easily observed in the first round.
System.out.printf("Get key %s in round %s%n", key, i);
}
}
}
private static class ConcurrencyTask implements Runnable {
private final Random random = new Random();
@Override
@SuppressWarnings("InfiniteLoopStatement")
public void run() {
System.out.println("Add loop started in task!");
while (true) {
holder.put(random.nextInt() % (1024 * 1024 * 100), null);
}
}
}
}