Added QuittingCompletable.java

This commit is contained in:
Bruce Eckel 2016-12-31 14:57:31 -08:00
parent 49ea4c8d4b
commit 8c4b4e7896
6 changed files with 66 additions and 29 deletions

View File

@ -9,7 +9,7 @@ import java.util.concurrent.*;
import java.nio.file.*; import java.nio.file.*;
public class ParallelStreamPuzzle2 { public class ParallelStreamPuzzle2 {
public static ConcurrentLinkedDeque<String> trace = public static Deque<String> trace =
new ConcurrentLinkedDeque<>(); new ConcurrentLinkedDeque<>();
static class static class
IntGenerator implements Supplier<Integer> { IntGenerator implements Supplier<Integer> {
@ -20,8 +20,10 @@ public class ParallelStreamPuzzle2 {
return current++; return current++;
} }
} }
public static void main(String[] args) throws Exception { public static void
List<Integer> x = Stream.generate(new IntGenerator()) main(String[] args) throws Exception {
List<Integer> x =
Stream.generate(new IntGenerator())
.limit(10) .limit(10)
.parallel() .parallel()
.collect(Collectors.toList()); .collect(Collectors.toList());

View File

@ -0,0 +1,35 @@
// concurrent/QuittingCompletable.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.stream.*;
import java.util.concurrent.*;
import onjava.Nap;
public class QuittingCompletable {
public static void main(String[] args) {
List<QuittableTask> tasks =
IntStream.range(1, QuittingTasks.COUNT)
.mapToObj(QuittableTask::new)
.collect(Collectors.toList());
List<CompletableFuture<Void>> cfutures =
tasks.stream()
.map(CompletableFuture::runAsync)
.collect(Collectors.toList());
new Nap(1000);
tasks.forEach(QuittableTask::quit);
cfutures.forEach(CompletableFuture::join);
}
}
/* Output:
125 148 115 127 120 118 106 140 77 119 97 80 143 17 92 147
89 123 16 12 138 25 13 101 135 96 76 73 130 133 37 132 134
149 137 122 29 49 60 40 142 131 53 1 98 145 126 65 5 64 82
79 68 86 141 61 128 22 7 26 19 139 114 146 14 15 43 34 10
75 87 90 31 47 38 62 18 63 41 42 144 66 23 6 4 91 70 83 102
103 54 69 74 56 71 94 88 78 81 57 52 93 45 48 44 32 28 36
33 104 105 112 109 100 117 24 108 21 116 20 9 85 8 84 72
107 113 121 124 136 129 99 95 55 3 27 2 59 67 50 58 51 39
30 35 46 110 111 11
*/

View File

@ -7,18 +7,18 @@ import java.util.function.*;
import onjava.Timer; import onjava.Timer;
public class Summing { public class Summing {
static volatile long x; static volatile long result;
static void timeTest(String id, long checkValue, static void timeTest(String id, long checkValue,
LongSupplier operation) { LongSupplier operation) {
System.out.print(id + ": "); System.out.print(id + ": ");
Timer timer = new Timer(); Timer timer = new Timer();
long result = operation.getAsLong(); // Prevent optimization:
result = operation.getAsLong();
if(result == checkValue) if(result == checkValue)
System.out.println(timer.duration() + "ms"); System.out.println(timer.duration() + "ms");
else else
System.out.format("result: %d%ncheckValue: %d%n", System.out.format("result: %d%ncheckValue: %d%n",
result, checkValue); result, checkValue);
x = result; // Prevent optimization
} }
public static final int SZ = 100_000_000; public static final int SZ = 100_000_000;
// This even works: // This even works:
@ -34,7 +34,7 @@ public class Summing {
timeTest("Sum Iterated", CHECK, () -> timeTest("Sum Iterated", CHECK, () ->
LongStream.iterate(0, i -> i + 1) LongStream.iterate(0, i -> i + 1)
.limit(SZ+1).sum()); .limit(SZ+1).sum());
// Takes longer, runs out of memory above 1_000_000: // Slower & runs out of memory above 1_000_000:
// timeTest("Sum Iterated Parallel", CHECK, () -> // timeTest("Sum Iterated Parallel", CHECK, () ->
// LongStream.iterate(0, i -> i + 1) // LongStream.iterate(0, i -> i + 1)
// .parallel() // .parallel()

View File

@ -36,8 +36,8 @@ public class Summing2 {
} }
/* Output: /* Output:
200000010000000 200000010000000
Array Stream Sum: 22ms Array Stream Sum: 104ms
Parallel: 21ms Parallel: 81ms
Basic Sum: 16ms Basic Sum: 106ms
parallelPrefix: 116ms parallelPrefix: 265ms
*/ */