-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronizedMapExample.java
More file actions
37 lines (33 loc) ยท 1.07 KB
/
SynchronizedMapExample.java
File metadata and controls
37 lines (33 loc) ยท 1.07 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
package ch15.sec07;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class SynchronizedMapExample {
public static void main(String[] args) throws InterruptedException {
Map<Integer, String> map = Collections.synchronizedMap(new HashMap<>());
Thread t1 = new Thread() {
@Override
public void run() {
for (int i = 1; i <= 1000; i++) {
map.put(i, "๋ด์ฉ" + i);
}
System.out.println(Thread.currentThread().getName() + ": " + map.size());
}
};
Thread t2 = new Thread() {
@Override
public void run() {
for (int i = 1001; i <= 2000; i++) {
map.put(i, "๋ด์ฉ" + i);
}
System.out.println(Thread.currentThread().getName() + ": " + map.size());
}
};
t1.start();
t2.start();
t1.join();
t2.join();
int size = map.size();
System.out.println("size = " + size);
}
}