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.*;
public class ParallelStreamPuzzle2 {
public static ConcurrentLinkedDeque<String> trace =
public static Deque<String> trace =
new ConcurrentLinkedDeque<>();
static class
IntGenerator implements Supplier<Integer> {
@ -20,11 +20,13 @@ public class ParallelStreamPuzzle2 {
return current++;
}
}
public static void main(String[] args) throws Exception {
List<Integer> x = Stream.generate(new IntGenerator())
.limit(10)
.parallel()
.collect(Collectors.toList());
public static void
main(String[] args) throws Exception {
List<Integer> x =
Stream.generate(new IntGenerator())
.limit(10)
.parallel()
.collect(Collectors.toList());
System.out.println(x);
Files.write(Paths.get("PSP2.txt"), trace);
}

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

View File

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

View File

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

View File

@ -10,20 +10,20 @@ import java.io.IOException;
public class RmDir {
public static void rmdir(Path dir) throws IOException {
Files.walkFileTree(dir,new SimpleFileVisitor<Path>() {
@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;
}
});
}
}