Skip to content

Commit bd6b0af

Browse files
committed
Added "Constructors are not Thread-Safe"
1 parent d360aa5 commit bd6b0af

File tree

7 files changed

+135
-1
lines changed

7 files changed

+135
-1
lines changed

concurrent/GuardedIDField.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
*/

concurrent/HasID.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}

concurrent/IDChecker.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
*/

concurrent/StaticIDField.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}

concurrent/TestStaticIDField.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
*/

gradle/subprojects.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ project(':collections') {
6767
configure(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
}

0 commit comments

Comments
 (0)