Skip to content

Commit 8c4b4e7

Browse files
author
Bruce Eckel
committed
Added QuittingCompletable.java
1 parent 49ea4c8 commit 8c4b4e7

6 files changed

Lines changed: 66 additions & 29 deletions

File tree

concurrent/ParallelStreamPuzzle2.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.nio.file.*;
1010

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

concurrent/Summing.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
import onjava.Timer;
88

99
public class Summing {
10-
static volatile long x;
10+
static volatile long result;
1111
static void timeTest(String id, long checkValue,
1212
LongSupplier operation) {
1313
System.out.print(id + ": ");
1414
Timer timer = new Timer();
15-
long result = operation.getAsLong();
15+
// Prevent optimization:
16+
result = operation.getAsLong();
1617
if(result == checkValue)
1718
System.out.println(timer.duration() + "ms");
1819
else
1920
System.out.format("result: %d%ncheckValue: %d%n",
2021
result, checkValue);
21-
x = result; // Prevent optimization
2222
}
2323
public static final int SZ = 100_000_000;
2424
// This even works:
@@ -34,7 +34,7 @@ public static void main(String[] args) {
3434
timeTest("Sum Iterated", CHECK, () ->
3535
LongStream.iterate(0, i -> i + 1)
3636
.limit(SZ+1).sum());
37-
// Takes longer, runs out of memory above 1_000_000:
37+
// Slower & runs out of memory above 1_000_000:
3838
// timeTest("Sum Iterated Parallel", CHECK, () ->
3939
// LongStream.iterate(0, i -> i + 1)
4040
// .parallel()

concurrent/Summing2.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public static void main(String[] args) {
3636
}
3737
/* Output:
3838
200000010000000
39-
Array Stream Sum: 22ms
40-
Parallel: 21ms
41-
Basic Sum: 16ms
42-
parallelPrefix: 116ms
39+
Array Stream Sum: 104ms
40+
Parallel: 81ms
41+
Basic Sum: 106ms
42+
parallelPrefix: 265ms
4343
*/

files/PathAnalysis.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static void say(String id, Object result) {
3232
if(Files.isSymbolicLink(p))
3333
say("SymbolicLink", Files.readSymbolicLink(p));
3434
if(FileSystems.getDefault()
35-
.supportedFileAttributeViews().contains("posix"))
35+
.supportedFileAttributeViews().contains("posix"))
3636
say("PosixFilePermissions",
3737
Files.getPosixFilePermissions(p));
3838
}

onjava/RmDir.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
public class RmDir {
1111
public static void rmdir(Path dir) throws IOException {
1212
Files.walkFileTree(dir,new SimpleFileVisitor<Path>() {
13-
@Override
14-
public FileVisitResult
15-
visitFile(Path file, BasicFileAttributes attrs)
16-
throws IOException {
17-
Files.delete(file);
18-
return FileVisitResult.CONTINUE;
19-
}
20-
@Override
21-
public FileVisitResult
22-
postVisitDirectory(Path dir, IOException exc)
23-
throws IOException {
24-
Files.delete(dir);
25-
return FileVisitResult.CONTINUE;
26-
}
13+
@Override
14+
public FileVisitResult
15+
visitFile(Path file, BasicFileAttributes attrs)
16+
throws IOException {
17+
Files.delete(file);
18+
return FileVisitResult.CONTINUE;
19+
}
20+
@Override
21+
public FileVisitResult
22+
postVisitDirectory(Path dir, IOException exc)
23+
throws IOException {
24+
Files.delete(dir);
25+
return FileVisitResult.CONTINUE;
26+
}
2727
});
2828
}
2929
}

0 commit comments

Comments
 (0)