diff --git a/concurrent/GuardedIDField.java b/concurrent/GuardedIDField.java new file mode 100644 index 00000000..0ab48d2e --- /dev/null +++ b/concurrent/GuardedIDField.java @@ -0,0 +1,18 @@ +// concurrent/GuardedIDField.java +// (c)2017 MindView LLC: see Copyright.txt +// We make no guarantees that this code is fit for any purpose. +// Visit http://OnJava8.com for more book information. +import java.util.concurrent.atomic.*; + +public class GuardedIDField implements HasID { + private static AtomicInteger counter = + new AtomicInteger(); + private int id = counter.getAndAdd(1); + public int getID() { return id; } + public static void main(String[] args) { + IDChecker.test(GuardedIDField::new); + } +} +/* Output: +0 +*/ diff --git a/concurrent/HasID.java b/concurrent/HasID.java new file mode 100644 index 00000000..4e679a59 --- /dev/null +++ b/concurrent/HasID.java @@ -0,0 +1,8 @@ +// concurrent/HasID.java +// (c)2017 MindView LLC: see Copyright.txt +// We make no guarantees that this code is fit for any purpose. +// Visit http://OnJava8.com for more book information. + +public interface HasID { + int getID(); +} diff --git a/concurrent/IDChecker.java b/concurrent/IDChecker.java new file mode 100644 index 00000000..e4f7cac5 --- /dev/null +++ b/concurrent/IDChecker.java @@ -0,0 +1,41 @@ +// concurrent/IDChecker.java +// (c)2017 MindView LLC: see Copyright.txt +// We make no guarantees that this code is fit for any purpose. +// Visit http://OnJava8.com for more book information. +import java.util.*; +import java.util.function.*; +import java.util.stream.*; +import java.util.concurrent.*; +import com.google.common.collect.Sets; + +public class IDChecker { + public static int SIZE = 100_000; + static class MakeObjects + implements Supplier> { + private Supplier gen; + public MakeObjects(Supplier gen) { + this.gen = gen; + } + @Override + public List get() { + return + Stream.generate(gen) + .limit(SIZE) + .map(HasID::getID) + .collect(Collectors.toList()); + } + } + public static void test(Supplier gen) { + CompletableFuture> + groupA = CompletableFuture + .supplyAsync(new MakeObjects(gen)), + groupB = CompletableFuture + .supplyAsync(new MakeObjects(gen)); + groupA.thenAcceptBoth(groupB, (a, b) -> { + System.out.println( + Sets.intersection( + Sets.newHashSet(a), + Sets.newHashSet(b)).size()); + }).join(); + } +} diff --git a/concurrent/SharedConstructorArgument.java b/concurrent/SharedConstructorArgument.java new file mode 100644 index 00000000..0cf835d8 --- /dev/null +++ b/concurrent/SharedConstructorArgument.java @@ -0,0 +1,44 @@ +// concurrent/SharedConstructorArgument.java +// (c)2017 MindView LLC: see Copyright.txt +// We make no guarantees that this code is fit for any purpose. +// Visit http://OnJava8.com for more book information. +import java.util.concurrent.atomic.*; + +interface SharedArg { + int get(); +} + +class Unsafe implements SharedArg { + private int i = 0; + public int get() { return i++; } +} + +class Safe implements SharedArg { + private static AtomicInteger counter = + new AtomicInteger(); + public int get() { + return counter.getAndAdd(1); + } +} + +class SharedUser implements HasID { + private final int id; + public SharedUser(SharedArg sa) { + id = sa.get(); + } + @Override + public int getID() { return id; } +} + +public class SharedConstructorArgument { + public static void main(String[] args) { + Unsafe us = new Unsafe(); + IDChecker.test(() -> new SharedUser(us)); + Safe sa = new Safe(); + IDChecker.test(() -> new SharedUser(sa)); + } +} +/* Output: +47747 +0 +*/ diff --git a/concurrent/StaticIDField.java b/concurrent/StaticIDField.java new file mode 100644 index 00000000..9e563dfd --- /dev/null +++ b/concurrent/StaticIDField.java @@ -0,0 +1,10 @@ +// concurrent/StaticIDField.java +// (c)2017 MindView LLC: see Copyright.txt +// We make no guarantees that this code is fit for any purpose. +// Visit http://OnJava8.com for more book information. + +public class StaticIDField implements HasID { + private static int counter = 0; + private int id = counter++; + public int getID() { return id; } +} diff --git a/concurrent/TestStaticIDField.java b/concurrent/TestStaticIDField.java new file mode 100644 index 00000000..5ad53adb --- /dev/null +++ b/concurrent/TestStaticIDField.java @@ -0,0 +1,13 @@ +// concurrent/TestStaticIDField.java +// (c)2017 MindView LLC: see Copyright.txt +// We make no guarantees that this code is fit for any purpose. +// Visit http://OnJava8.com for more book information. + +public class TestStaticIDField { + public static void main(String[] args) { + IDChecker.test(StaticIDField::new); + } +} +/* Output: +47643 +*/ diff --git a/gradle/subprojects.gradle b/gradle/subprojects.gradle index d22a7cff..e114237d 100644 --- a/gradle/subprojects.gradle +++ b/gradle/subprojects.gradle @@ -67,7 +67,7 @@ project(':collections') { configure(subprojects - project(':onjava')) { dependencies { compile project(':onjava') - compile 'com.google.guava:guava:21.0-rc2' + compile 'com.google.guava:guava:21.0' compileOnly "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}" } }