File tree Expand file tree Collapse file tree 7 files changed +135
-1
lines changed
Expand file tree Collapse file tree 7 files changed +135
-1
lines changed Original file line number Diff line number Diff line change 1+ // concurrent/GuardedIDField.java
2+ // (c)2017 MindView LLC: see Copyright.txt
3+ // We make no guarantees that this code is fit for any purpose.
4+ // Visit http://OnJava8.com for more book information.
5+ import java .util .concurrent .atomic .*;
6+
7+ public class GuardedIDField implements HasID {
8+ private static AtomicInteger counter =
9+ new AtomicInteger ();
10+ private int id = counter .getAndAdd (1 );
11+ public int getID () { return id ; }
12+ public static void main (String [] args ) {
13+ IDChecker .test (GuardedIDField ::new );
14+ }
15+ }
16+ /* Output:
17+ 0
18+ */
Original file line number Diff line number Diff line change 1+ // concurrent/HasID.java
2+ // (c)2017 MindView LLC: see Copyright.txt
3+ // We make no guarantees that this code is fit for any purpose.
4+ // Visit http://OnJava8.com for more book information.
5+
6+ public interface HasID {
7+ int getID ();
8+ }
Original file line number Diff line number Diff line change 1+ // concurrent/IDChecker.java
2+ // (c)2017 MindView LLC: see Copyright.txt
3+ // We make no guarantees that this code is fit for any purpose.
4+ // Visit http://OnJava8.com for more book information.
5+ import java .util .*;
6+ import java .util .function .*;
7+ import java .util .stream .*;
8+ import java .util .concurrent .*;
9+ import com .google .common .collect .Sets ;
10+
11+ public class IDChecker {
12+ public static int SIZE = 100_000 ;
13+ static class MakeObjects
14+ implements Supplier <List <Integer >> {
15+ private Supplier <HasID > gen ;
16+ public MakeObjects (Supplier <HasID > gen ) {
17+ this .gen = gen ;
18+ }
19+ @ Override
20+ public List <Integer > get () {
21+ return
22+ Stream .generate (gen )
23+ .limit (SIZE )
24+ .map (HasID ::getID )
25+ .collect (Collectors .toList ());
26+ }
27+ }
28+ public static void test (Supplier <HasID > gen ) {
29+ CompletableFuture <List <Integer >>
30+ groupA = CompletableFuture
31+ .supplyAsync (new MakeObjects (gen )),
32+ groupB = CompletableFuture
33+ .supplyAsync (new MakeObjects (gen ));
34+ groupA .thenAcceptBoth (groupB , (a , b ) -> {
35+ System .out .println (
36+ Sets .intersection (
37+ Sets .newHashSet (a ),
38+ Sets .newHashSet (b )).size ());
39+ }).join ();
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ // concurrent/SharedConstructorArgument.java
2+ // (c)2017 MindView LLC: see Copyright.txt
3+ // We make no guarantees that this code is fit for any purpose.
4+ // Visit http://OnJava8.com for more book information.
5+ import java .util .concurrent .atomic .*;
6+
7+ interface SharedArg {
8+ int get ();
9+ }
10+
11+ class Unsafe implements SharedArg {
12+ private int i = 0 ;
13+ public int get () { return i ++; }
14+ }
15+
16+ class Safe implements SharedArg {
17+ private static AtomicInteger counter =
18+ new AtomicInteger ();
19+ public int get () {
20+ return counter .getAndAdd (1 );
21+ }
22+ }
23+
24+ class SharedUser implements HasID {
25+ private final int id ;
26+ public SharedUser (SharedArg sa ) {
27+ id = sa .get ();
28+ }
29+ @ Override
30+ public int getID () { return id ; }
31+ }
32+
33+ public class SharedConstructorArgument {
34+ public static void main (String [] args ) {
35+ Unsafe us = new Unsafe ();
36+ IDChecker .test (() -> new SharedUser (us ));
37+ Safe sa = new Safe ();
38+ IDChecker .test (() -> new SharedUser (sa ));
39+ }
40+ }
41+ /* Output:
42+ 47747
43+ 0
44+ */
Original file line number Diff line number Diff line change 1+ // concurrent/StaticIDField.java
2+ // (c)2017 MindView LLC: see Copyright.txt
3+ // We make no guarantees that this code is fit for any purpose.
4+ // Visit http://OnJava8.com for more book information.
5+
6+ public class StaticIDField implements HasID {
7+ private static int counter = 0 ;
8+ private int id = counter ++;
9+ public int getID () { return id ; }
10+ }
Original file line number Diff line number Diff line change 1+ // concurrent/TestStaticIDField.java
2+ // (c)2017 MindView LLC: see Copyright.txt
3+ // We make no guarantees that this code is fit for any purpose.
4+ // Visit http://OnJava8.com for more book information.
5+
6+ public class TestStaticIDField {
7+ public static void main (String [] args ) {
8+ IDChecker .test (StaticIDField ::new );
9+ }
10+ }
11+ /* Output:
12+ 47643
13+ */
Original file line number Diff line number Diff line change @@ -67,7 +67,7 @@ project(':collections') {
6767configure(subprojects - project(' :onjava' )) {
6868 dependencies {
6969 compile project(' :onjava' )
70- compile ' com.google.guava:guava:21.0-rc2 '
70+ compile ' com.google.guava:guava:21.0'
7171 compileOnly " org.openjdk.jmh:jmh-core:${ jmh.jmhVersion} "
7272 }
7373}
You can’t perform that action at this time.
0 commit comments