5
5
import java .util .concurrent .BlockingQueue ;
6
6
import java .util .concurrent .ConcurrentHashMap ;
7
7
import java .util .concurrent .CopyOnWriteArrayList ;
8
+ import java .util .concurrent .ExecutorService ;
9
+ import java .util .concurrent .Executors ;
8
10
import java .util .concurrent .LinkedBlockingQueue ;
11
+ import java .util .concurrent .TimeUnit ;
9
12
10
13
/**
11
14
* Concurrent collections are an alternative to the Synchronized Collections.
@@ -35,6 +38,7 @@ public class UsingConcurrentCollections {
35
38
* - Just a few Writers can modify it.
36
39
*/
37
40
public static void usingConcurrentHashMap () {
41
+ ExecutorService executor = Executors .newCachedThreadPool ();
38
42
System .out .println ("=== ConcurrentHashMap ===" );
39
43
Random random = new Random ();
40
44
ConcurrentHashMap <UUID , Integer > valuesPerUuid = new ConcurrentHashMap <>();
@@ -45,18 +49,22 @@ public static void usingConcurrentHashMap() {
45
49
for (int i = 0 ; i < 100 ; i ++) {
46
50
if (i % 6 == 0 ) {
47
51
// write
48
- new Thread (() -> {
52
+ executor . execute (() -> {
49
53
UUID uuid = UUID .randomUUID ();
50
54
Integer value = random .nextInt (10 );
51
55
System .out .println ("Added " + uuid + " - " + value );
52
56
valuesPerUuid .putIfAbsent (uuid , value );
53
- }). start () ;
57
+ });
54
58
} else {
55
59
// read
56
- new Thread (() -> System .out .println ("Printed " + valuesPerUuid .values ().toString ())). start ( );
60
+ executor . execute (() -> System .out .println ("Printed " + valuesPerUuid .values ().toString ()));
57
61
}
58
62
}
63
+
64
+ // Finishing
65
+ executor .shutdown ();
59
66
try {
67
+ executor .awaitTermination (2000 , TimeUnit .SECONDS );
60
68
// space for other examples
61
69
Thread .sleep (2000 );
62
70
System .out .println ("\n \n \n \n " );
@@ -79,6 +87,7 @@ public static void usingConcurrentHashMap() {
79
87
*
80
88
*/
81
89
public static void usingCopyOnWriteArrayList () {
90
+ ExecutorService executor = Executors .newCachedThreadPool ();
82
91
System .out .println ("=== CopyOnWriteArrayList ===" );
83
92
Random random = new Random ();
84
93
// No ConcurrentModificationException
@@ -87,23 +96,27 @@ public static void usingCopyOnWriteArrayList() {
87
96
for (int i = 0 ; i < 100 ; i ++) {
88
97
if (i % 8 == 0 ) {
89
98
// write
90
- new Thread (() -> {
99
+ executor . execute (() -> {
91
100
Integer value = random .nextInt (10 );
92
101
System .err .println ("Added " + value );
93
102
copyOnWriteArrayList .add (value );
94
- }). start () ;
103
+ });
95
104
} else {
96
105
// read
97
- new Thread (() -> {
106
+ executor . execute (() -> {
98
107
StringBuilder sb = new StringBuilder ();
99
108
for (Integer vv : copyOnWriteArrayList ) {
100
109
sb .append (vv + " " );
101
110
}
102
111
System .out .println ("Reading " + sb .toString ());
103
- }). start () ;
112
+ });
104
113
}
105
114
}
115
+
116
+ // Finishing
117
+ executor .shutdown ();
106
118
try {
119
+ executor .awaitTermination (2000 , TimeUnit .SECONDS );
107
120
// space for other examples
108
121
Thread .sleep (2000 );
109
122
System .out .println ("\n \n \n \n " );
@@ -168,7 +181,7 @@ public static void usingBlockingQueue() {
168
181
}
169
182
};
170
183
171
- // Multiple producers
184
+ // Multiple producers - Examples using simple threads this time.
172
185
Thread producer1 = new Thread (runProducer );
173
186
producer1 .start ();
174
187
Thread producer2 = new Thread (runProducer );
0 commit comments