forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapSafe.java
More file actions
32 lines (27 loc) · 819 Bytes
/
MapSafe.java
File metadata and controls
32 lines (27 loc) · 819 Bytes
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
/**
* @program JavaBooks
* @description: MapSafe
* @author: mf
* @create: 2020/02/14 17:24
*/
package com.juc.collectiontest;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class MapSafe {
public static void main(String[] args) {
notSafe();
}
public static void notSafe() {
// Map<String, String> map = new HashMap<>();
// Map<String, String> map = new ConcurrentHashMap<>();
Map<String, String> map = Collections.synchronizedMap(new HashMap<>());
for (int i = 1; i <= 30; i++) {
new Thread(() -> {
map.put(Thread.currentThread().getName(), UUID.randomUUID().toString());
System.out.println(map);
}, "Thread " + i).start();
}
}
}