Added "Constructors are not Thread-Safe"

This commit is contained in:
Bruce Eckel 2017-01-13 14:28:08 -08:00
parent d360aa59e7
commit bd6b0af400
7 changed files with 135 additions and 1 deletions

View File

@ -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
*/

8
concurrent/HasID.java Normal file
View File

@ -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();
}

41
concurrent/IDChecker.java Normal file
View File

@ -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<List<Integer>> {
private Supplier<HasID> gen;
public MakeObjects(Supplier<HasID> gen) {
this.gen = gen;
}
@Override
public List<Integer> get() {
return
Stream.generate(gen)
.limit(SIZE)
.map(HasID::getID)
.collect(Collectors.toList());
}
}
public static void test(Supplier<HasID> gen) {
CompletableFuture<List<Integer>>
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();
}
}

View File

@ -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
*/

View File

@ -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; }
}

View File

@ -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
*/

View File

@ -67,7 +67,7 @@ project(':collections') {
configure(subprojects - project(':onjava')) { configure(subprojects - project(':onjava')) {
dependencies { dependencies {
compile project(':onjava') 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}" compileOnly "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}"
} }
} }