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