-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronizedDemo.java
More file actions
38 lines (28 loc) · 828 Bytes
/
SynchronizedDemo.java
File metadata and controls
38 lines (28 loc) · 828 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
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;
public class SynchronizedDemo extends ConcurrentUtils {
static int count = 0;
/* Work too
* static synchronized void incrementSync() { count = count + 1; }
*/
static void incrementSync() {
synchronized (SynchronizedDemo.class) {
count = count + 1;
}
}
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
/* work too
IntStream.range(0, 10000).forEach(
i -> executor.submit(() ->
{
synchronized(SynchronizedDemo.class){count=count+1;}
}));
*/
IntStream.range(0, 10000).forEach(
i -> executor.submit(SynchronizedDemo::incrementSync));
stop(executor);
System.out.println(count); // 10000
}
}