File tree 1 file changed +39
-0
lines changed
src/com/winterbe/java8/samples/concurrent 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .winterbe .java8 .samples .concurrent ;
2
+
3
+ import java .util .concurrent .ExecutorService ;
4
+ import java .util .concurrent .Executors ;
5
+ import java .util .stream .IntStream ;
6
+
7
+ /**
8
+ * @author Benjamin Winterberg
9
+ */
10
+ public class Synchronized2 {
11
+
12
+ private static final int NUM_INCREMENTS = 10000 ;
13
+
14
+ private static int count = 0 ;
15
+
16
+ public static void main (String [] args ) {
17
+ testSyncIncrement ();
18
+ }
19
+
20
+ private static void testSyncIncrement () {
21
+ count = 0 ;
22
+
23
+ ExecutorService executor = Executors .newFixedThreadPool (2 );
24
+
25
+ IntStream .range (0 , NUM_INCREMENTS )
26
+ .forEach (i -> executor .submit (Synchronized2 ::incrementSync ));
27
+
28
+ ConcurrentUtils .stop (executor );
29
+
30
+ System .out .println (count );
31
+ }
32
+
33
+ private static void incrementSync () {
34
+ synchronized (Synchronized2 .class ) {
35
+ count = count + 1 ;
36
+ }
37
+ }
38
+
39
+ }
You can’t perform that action at this time.
0 commit comments