File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
src/com/winterbe/java8/samples/concurrent Expand file tree Collapse file tree 1 file changed +31
-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 .concurrent .atomic .LongAccumulator ;
6+ import java .util .stream .IntStream ;
7+
8+ /**
9+ * @author Benjamin Winterberg
10+ */
11+ public class LongAccumulator1 {
12+
13+ private static final int SIZE = 10 ;
14+
15+ private static LongAccumulator accumulator = new LongAccumulator ((x , y ) -> 2 * x + y , 1L );
16+
17+ public static void main (String [] args ) {
18+ testAccumulate ();
19+ }
20+
21+ private static void testAccumulate () {
22+ ExecutorService executor = Executors .newFixedThreadPool (2 );
23+
24+ IntStream .range (0 , SIZE )
25+ .forEach (i -> executor .submit (() -> accumulator .accumulate (i )));
26+
27+ ConcurrentUtils .stop (executor );
28+
29+ System .out .format ("Add: %d\n " , accumulator .getThenReset ());
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments