From 8c4b4e789618c273e258f13680a69fe9d517b88b Mon Sep 17 00:00:00 2001 From: Bruce Eckel Date: Sat, 31 Dec 2016 14:57:31 -0800 Subject: [PATCH] Added QuittingCompletable.java --- concurrent/ParallelStreamPuzzle2.java | 14 ++++++----- concurrent/QuittingCompletable.java | 35 +++++++++++++++++++++++++++ concurrent/Summing.java | 8 +++--- concurrent/Summing2.java | 8 +++--- files/PathAnalysis.java | 2 +- onjava/RmDir.java | 28 ++++++++++----------- 6 files changed, 66 insertions(+), 29 deletions(-) create mode 100644 concurrent/QuittingCompletable.java diff --git a/concurrent/ParallelStreamPuzzle2.java b/concurrent/ParallelStreamPuzzle2.java index 237af9e2..f1fb00cf 100644 --- a/concurrent/ParallelStreamPuzzle2.java +++ b/concurrent/ParallelStreamPuzzle2.java @@ -9,7 +9,7 @@ import java.util.concurrent.*; import java.nio.file.*; public class ParallelStreamPuzzle2 { - public static ConcurrentLinkedDeque trace = + public static Deque trace = new ConcurrentLinkedDeque<>(); static class IntGenerator implements Supplier { @@ -20,11 +20,13 @@ public class ParallelStreamPuzzle2 { return current++; } } - public static void main(String[] args) throws Exception { - List x = Stream.generate(new IntGenerator()) - .limit(10) - .parallel() - .collect(Collectors.toList()); + public static void + main(String[] args) throws Exception { + List x = + Stream.generate(new IntGenerator()) + .limit(10) + .parallel() + .collect(Collectors.toList()); System.out.println(x); Files.write(Paths.get("PSP2.txt"), trace); } diff --git a/concurrent/QuittingCompletable.java b/concurrent/QuittingCompletable.java new file mode 100644 index 00000000..539c0998 --- /dev/null +++ b/concurrent/QuittingCompletable.java @@ -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 tasks = + IntStream.range(1, QuittingTasks.COUNT) + .mapToObj(QuittableTask::new) + .collect(Collectors.toList()); + List> 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 +*/ diff --git a/concurrent/Summing.java b/concurrent/Summing.java index 14202a52..937a4898 100644 --- a/concurrent/Summing.java +++ b/concurrent/Summing.java @@ -7,18 +7,18 @@ import java.util.function.*; import onjava.Timer; public class Summing { - static volatile long x; + static volatile long result; static void timeTest(String id, long checkValue, LongSupplier operation) { System.out.print(id + ": "); Timer timer = new Timer(); - long result = operation.getAsLong(); + // Prevent optimization: + result = operation.getAsLong(); if(result == checkValue) System.out.println(timer.duration() + "ms"); else System.out.format("result: %d%ncheckValue: %d%n", result, checkValue); - x = result; // Prevent optimization } public static final int SZ = 100_000_000; // This even works: @@ -34,7 +34,7 @@ public class Summing { timeTest("Sum Iterated", CHECK, () -> LongStream.iterate(0, i -> i + 1) .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, () -> // LongStream.iterate(0, i -> i + 1) // .parallel() diff --git a/concurrent/Summing2.java b/concurrent/Summing2.java index c6ea6179..a361a660 100644 --- a/concurrent/Summing2.java +++ b/concurrent/Summing2.java @@ -36,8 +36,8 @@ public class Summing2 { } /* Output: 200000010000000 -Array Stream Sum: 22ms -Parallel: 21ms -Basic Sum: 16ms -parallelPrefix: 116ms +Array Stream Sum: 104ms +Parallel: 81ms +Basic Sum: 106ms +parallelPrefix: 265ms */ diff --git a/files/PathAnalysis.java b/files/PathAnalysis.java index d1de4415..b76be551 100644 --- a/files/PathAnalysis.java +++ b/files/PathAnalysis.java @@ -32,7 +32,7 @@ public class PathAnalysis { if(Files.isSymbolicLink(p)) say("SymbolicLink", Files.readSymbolicLink(p)); if(FileSystems.getDefault() - .supportedFileAttributeViews().contains("posix")) + .supportedFileAttributeViews().contains("posix")) say("PosixFilePermissions", Files.getPosixFilePermissions(p)); } diff --git a/onjava/RmDir.java b/onjava/RmDir.java index 89a495d4..45cae6d2 100644 --- a/onjava/RmDir.java +++ b/onjava/RmDir.java @@ -10,20 +10,20 @@ import java.io.IOException; public class RmDir { public static void rmdir(Path dir) throws IOException { Files.walkFileTree(dir,new SimpleFileVisitor() { - @Override - public FileVisitResult - visitFile(Path file, BasicFileAttributes attrs) - throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } - @Override - public FileVisitResult - postVisitDirectory(Path dir, IOException exc) - throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; - } + @Override + public FileVisitResult + visitFile(Path file, BasicFileAttributes attrs) + throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + @Override + public FileVisitResult + postVisitDirectory(Path dir, IOException exc) + throws IOException { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } }); } }