Skip to content

Commit afa51c5

Browse files
author
Bruce Eckel
committed
Moved examples from "Concurrent" to "lowlevel"
1 parent c590aeb commit afa51c5

27 files changed

+623
-231
lines changed

concurrent/Baked.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// concurrent/Baked.java
2+
// (c)2016 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.*;
6+
import java.util.stream.*;
7+
import onjava.Nap;
8+
9+
public class Baked {
10+
static class Pan {}
11+
static Pan pan(Batter b) {
12+
new Nap(100);
13+
return new Pan();
14+
}
15+
static Baked heat(Pan p) {
16+
new Nap(100);
17+
return new Baked();
18+
}
19+
static CompletableFuture<Baked>
20+
bake(CompletableFuture<Batter> cfb) {
21+
return cfb
22+
.thenApplyAsync(Baked::pan)
23+
.thenApplyAsync(Baked::heat);
24+
}
25+
public static
26+
Stream<CompletableFuture<Baked>> batch() {
27+
CompletableFuture<Batter> batter = Batter.mix();
28+
return Stream.of(bake(batter), bake(batter),
29+
bake(batter), bake(batter));
30+
}
31+
}

concurrent/Batter.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// concurrent/Batter.java
2+
// (c)2016 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.*;
6+
import onjava.Nap;
7+
8+
public class Batter {
9+
static class Eggs {}
10+
static class Milk {}
11+
static class Sugar {}
12+
static class Flour {}
13+
static <T> T prepare(T ingredient) {
14+
new Nap(100);
15+
return ingredient;
16+
}
17+
static <T> CompletableFuture<T> cf(T ingredient) {
18+
return CompletableFuture
19+
.completedFuture(ingredient)
20+
.thenApply(Batter::prepare);
21+
}
22+
public static CompletableFuture<Batter> mix() {
23+
CompletableFuture<Eggs> eggs = cf(new Eggs());
24+
CompletableFuture<Milk> milk = cf(new Milk());
25+
CompletableFuture<Sugar> sugar = cf(new Sugar());
26+
CompletableFuture<Flour> flour = cf(new Flour());
27+
CompletableFuture
28+
.allOf(eggs, milk, sugar, flour)
29+
.join();
30+
new Nap(100); // Mixing time
31+
return
32+
CompletableFuture.completedFuture(new Batter());
33+
}
34+
}

concurrent/Breakable.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// concurrent/Breakable.java
2+
// (c)2016 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.*;
6+
7+
public class Breakable {
8+
String id;
9+
private int failcount;
10+
public Breakable(String id, int failcount) {
11+
this.id = id;
12+
this.failcount = failcount;
13+
}
14+
@Override
15+
public String toString() {
16+
return "Breakable_" + id +
17+
" [" + failcount + "]";
18+
}
19+
public static Breakable work(Breakable b) {
20+
if(--b.failcount == 0)
21+
throw new RuntimeException(
22+
"Breakable_" + b.id + " failed");
23+
System.out.println(b);
24+
return b;
25+
}
26+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// concurrent/CatchCompletableExceptions.java
2+
// (c)2016 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.*;
6+
import onjava.Nap;
7+
8+
public class CatchCompletableExceptions {
9+
static void handleException(int failcount) {
10+
// Call Function only if there's an exception,
11+
// Must produce same type as came in:
12+
CompletableExceptions
13+
.test("exceptionally", failcount)
14+
.exceptionally((ex) -> { // Function
15+
if(ex == null)
16+
System.out.println("I don't get it yet");
17+
return new Breakable(ex.getMessage(), 0);
18+
})
19+
.thenAccept(str ->
20+
System.out.println("result: " + str));
21+
22+
// Create a new result (recover):
23+
CompletableExceptions
24+
.test("handle", failcount)
25+
.handle((result, fail) -> { // BiFunction
26+
if(fail != null)
27+
return "Failure recovery object";
28+
else
29+
return result + " is good";
30+
})
31+
.thenAccept(str ->
32+
System.out.println("result: " + str));
33+
34+
// Do something but pass the same result through:
35+
CompletableExceptions
36+
.test("whenComplete", failcount)
37+
.whenComplete((result, fail) -> { // BiConsumer
38+
if(fail != null)
39+
System.out.println("It failed");
40+
else
41+
System.out.println(result + " OK");
42+
})
43+
.thenAccept(r ->
44+
System.out.println("result: " + r));
45+
}
46+
public static void main(String[] args) {
47+
System.out.println("**** Failure Mode ****");
48+
handleException(2);
49+
System.out.println("**** Success Mode ****");
50+
handleException(0);
51+
}
52+
}
53+
/* Output:
54+
**** Failure Mode ****
55+
Breakable_exceptionally [1]
56+
result: Breakable_java.lang.RuntimeException:
57+
Breakable_exceptionally failed [0]
58+
Breakable_handle [1]
59+
result: Failure recovery object
60+
Breakable_whenComplete [1]
61+
It failed
62+
**** Success Mode ****
63+
Breakable_exceptionally [-1]
64+
Breakable_exceptionally [-2]
65+
Breakable_exceptionally [-3]
66+
Breakable_exceptionally [-4]
67+
result: Breakable_exceptionally [-4]
68+
Breakable_handle [-1]
69+
Breakable_handle [-2]
70+
Breakable_handle [-3]
71+
Breakable_handle [-4]
72+
result: Breakable_handle [-4] is good
73+
Breakable_whenComplete [-1]
74+
Breakable_whenComplete [-2]
75+
Breakable_whenComplete [-3]
76+
Breakable_whenComplete [-4]
77+
Breakable_whenComplete [-4] OK
78+
result: Breakable_whenComplete [-4]
79+
*/

concurrent/Chopstick.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

concurrent/CompletableApply.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ public static void main(String[] args) {
1919
cf4.thenApply(Machina::work);
2020
}
2121
}
22+
/* Output:
23+
Machina0: ONE
24+
Machina0: TWO
25+
Machina0: THREE
26+
Machina0: complete
27+
*/
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// concurrent/CompletableExceptions.java
2+
// (c)2016 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.*;
6+
import onjava.Nap;
7+
8+
public class CompletableExceptions {
9+
static CompletableFuture<Breakable>
10+
test(String id, int failcount) {
11+
return
12+
CompletableFuture.completedFuture(
13+
new Breakable(id, failcount))
14+
.thenApply(Breakable::work)
15+
.thenApply(Breakable::work)
16+
.thenApply(Breakable::work)
17+
.thenApply(Breakable::work);
18+
}
19+
public static void main(String[] args) {
20+
// Exceptions don't appear ...
21+
test("A", 1);
22+
test("B", 2);
23+
test("C", 3);
24+
test("D", 4);
25+
test("E", 5);
26+
// ... until you try to fetch the value:
27+
try {
28+
test("F", 2).get(); // or join()
29+
} catch(Exception e) {
30+
System.out.println(e.getMessage());
31+
}
32+
// Test for exceptions:
33+
System.out.println(
34+
test("G", 2).isCompletedExceptionally());
35+
// Counts as "done":
36+
System.out.println(test("H", 2).isDone());
37+
// Force an exception:
38+
CompletableFuture<Integer> cfi =
39+
new CompletableFuture<>();
40+
System.out.println("done? " + cfi.isDone());
41+
cfi.completeExceptionally(
42+
new RuntimeException("forced"));
43+
try {
44+
cfi.get();
45+
} catch(Exception e) {
46+
System.out.println(e.getMessage());
47+
}
48+
}
49+
}
50+
/* Output:
51+
Breakable_B [1]
52+
Breakable_C [2]
53+
Breakable_C [1]
54+
Breakable_D [3]
55+
Breakable_D [2]
56+
Breakable_D [1]
57+
Breakable_E [4]
58+
Breakable_E [3]
59+
Breakable_E [2]
60+
Breakable_E [1]
61+
Breakable_F [1]
62+
java.lang.RuntimeException: Breakable_F failed
63+
Breakable_G [1]
64+
true
65+
Breakable_H [1]
66+
true
67+
done? false
68+
java.lang.RuntimeException: forced
69+
*/

concurrent/CompletableOperations.java

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import java.util.concurrent.*;
66

77
public class CompletableOperations {
8-
static CompletableFuture<Integer> cfi() {
8+
static CompletableFuture<Integer> cfi(int i) {
99
return
1010
CompletableFuture.completedFuture(
11-
new Integer(1));
11+
new Integer(i));
1212
}
1313
// Get and show value stored in a CF:
1414
static void showr(CompletableFuture<Integer> c) {
@@ -29,11 +29,60 @@ static void voidr(CompletableFuture<Void> c) {
2929
}
3030
}
3131
public static void main(String[] args) {
32-
showr(cfi()); // Basic test
33-
showr(cfi().thenApplyAsync(i -> i + 42));
34-
voidr(cfi().runAsync(() -> System.out.println("run")));
35-
CompletableFuture<Integer> c = cfi();
32+
showr(cfi(1)); // Basic test
33+
voidr(cfi(2).runAsync(() ->
34+
System.out.println("runAsync")));
35+
voidr(cfi(3).thenRunAsync(() ->
36+
System.out.println("thenRunAsync")));
37+
showr(CompletableFuture.supplyAsync(() -> 99));
38+
voidr(cfi(4).thenAcceptAsync(i ->
39+
System.out.println("thenAcceptAsync: " + i)));
40+
showr(cfi(5).thenApplyAsync(i -> i + 42));
41+
showr(cfi(6).thenComposeAsync(i -> cfi(i + 99)));
42+
CompletableFuture<Integer> c = cfi(7);
3643
c.obtrudeValue(111);
3744
showr(c);
45+
showr(cfi(8).toCompletableFuture());
46+
c = new CompletableFuture<>();
47+
c.complete(9);
48+
showr(c);
49+
c = new CompletableFuture<>();
50+
c.cancel(true);
51+
System.out.println("cancelled: " +
52+
c.isCancelled());
53+
System.out.println("completed exceptionally: " +
54+
c.isCompletedExceptionally());
55+
System.out.println("done: " + c.isDone());
56+
System.out.println(c);
57+
c = new CompletableFuture<>();
58+
System.out.println(c.getNow(777));
59+
c = new CompletableFuture<>();
60+
c.thenApplyAsync(i -> i + 42)
61+
.thenApplyAsync(i -> i * 12);
62+
System.out.println("dependents: " +
63+
c.getNumberOfDependents());
64+
c.thenApplyAsync(i -> i / 2);
65+
System.out.println("dependents: " +
66+
c.getNumberOfDependents());
3867
}
3968
}
69+
/* Output:
70+
1
71+
runAsync
72+
thenRunAsync
73+
99
74+
thenAcceptAsync: 4
75+
47
76+
105
77+
111
78+
8
79+
9
80+
cancelled: true
81+
completed exceptionally: true
82+
done: true
83+
java.util.concurrent.CompletableFuture@404b9385[Completed
84+
exceptionally]
85+
777
86+
dependents: 1
87+
dependents: 2
88+
*/

concurrent/CompletablePizza.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,47 @@ public static void main(String[] args) {
4242
}
4343
}
4444
/* Output:
45+
/* Output:
46+
56
47+
Pizza 0: ROLLED
48+
Pizza 1: ROLLED
49+
Pizza 4: ROLLED
50+
Pizza 2: ROLLED
51+
Pizza 3: ROLLED
52+
Pizza 0: SAUCED
53+
Pizza 3: SAUCED
54+
Pizza 2: SAUCED
55+
Pizza 4: SAUCED
56+
Pizza 1: SAUCED
57+
Pizza 0: CHEESED
58+
Pizza 4: CHEESED
59+
Pizza 2: CHEESED
60+
Pizza 3: CHEESED
61+
Pizza 1: CHEESED
62+
Pizza 0: TOPPED
63+
Pizza 4: TOPPED
64+
Pizza 1: TOPPED
65+
Pizza 3: TOPPED
66+
Pizza 2: TOPPED
67+
Pizza 0: BAKED
68+
Pizza 4: BAKED
69+
Pizza 3: BAKED
70+
Pizza 1: BAKED
71+
Pizza 2: BAKED
72+
Pizza 0: SLICED
73+
Pizza 2: SLICED
74+
Pizza 4: SLICED
75+
Pizza 1: SLICED
76+
Pizza 3: SLICED
77+
Pizza 0: BOXED
78+
Pizza0: complete
79+
Pizza 4: BOXED
80+
Pizza 3: BOXED
81+
Pizza 2: BOXED
82+
Pizza 1: BOXED
83+
Pizza1: complete
84+
Pizza2: complete
85+
Pizza3: complete
86+
Pizza4: complete
87+
1659
4588
*/

0 commit comments

Comments
 (0)