-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronizedCollection.java
More file actions
43 lines (29 loc) · 854 Bytes
/
Copy pathSynchronizedCollection.java
File metadata and controls
43 lines (29 loc) · 854 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
33
34
35
36
37
38
39
40
41
42
43
package concurrentCollection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SynchronizedCollection {
public static void main(String[] args) {
List<Integer> list= Collections.synchronizedList(new ArrayList<>());
final Object lock=new Object();
Thread one=new Thread(()->{
for (int i = 0; i < 1000; i++) {
list.add(i);
}
});
Thread two=new Thread(()->{
for (int i = 0; i < 1000; i++) {
list.add(i);
}
});
one.start();
two.start();
try {
one.join();
two.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(list.size());
}
}