< prev index next >

test/jdk/java/util/concurrent/StructuredTaskScope/StressCancellation.java

Print this page

 50         );
 51         // 0..15 forks before shutdown, 0..15 forks after shutdown
 52         return factories.flatMap(f -> IntStream.range(0, 256)
 53                 .mapToObj(x -> Arguments.of(f, x & 0x0F, (x & 0xF0) >> 4)));
 54     }
 55 
 56     /**
 57      * Test StructuredTaskScope cancellation with running threads and concurrently with
 58      * threads that are starting. The cancellation should interrupt all running threads,
 59      * join should wakeup, and close would complete quickly.
 60      *
 61      * @param factory the ThreadFactory to use
 62      * @param beforeCancel the number of subtasks to fork before cancel
 63      * @param afterCancel the number of subtasks to fork after cancel
 64      */
 65     @ParameterizedTest
 66     @MethodSource("testCases")
 67     void test(ThreadFactory factory, int beforeCancel, int afterCancel) throws Exception {
 68         var joiner = new Joiner<Boolean, Void>() {
 69             @Override
 70             public boolean onComplete(Subtask<? extends Boolean> subtask) {
 71                 boolean cancel = subtask.get();
 72                 return cancel;
 73             }
 74             @Override
 75             public Void result() {
 76                 return null;
 77             }
 78         };
 79 
 80         try (var scope = StructuredTaskScope.open(joiner, cf -> cf.withThreadFactory(factory))) {
 81             // fork subtasks
 82             for (int i = 0; i < beforeCancel; i++) {
 83                 scope.fork(() -> {
 84                     Thread.sleep(Duration.ofDays(1));
 85                     return false;
 86                 });
 87             }
 88 
 89             // fork subtask to cancel
 90             scope.fork(() -> true);

 50         );
 51         // 0..15 forks before shutdown, 0..15 forks after shutdown
 52         return factories.flatMap(f -> IntStream.range(0, 256)
 53                 .mapToObj(x -> Arguments.of(f, x & 0x0F, (x & 0xF0) >> 4)));
 54     }
 55 
 56     /**
 57      * Test StructuredTaskScope cancellation with running threads and concurrently with
 58      * threads that are starting. The cancellation should interrupt all running threads,
 59      * join should wakeup, and close would complete quickly.
 60      *
 61      * @param factory the ThreadFactory to use
 62      * @param beforeCancel the number of subtasks to fork before cancel
 63      * @param afterCancel the number of subtasks to fork after cancel
 64      */
 65     @ParameterizedTest
 66     @MethodSource("testCases")
 67     void test(ThreadFactory factory, int beforeCancel, int afterCancel) throws Exception {
 68         var joiner = new Joiner<Boolean, Void>() {
 69             @Override
 70             public boolean onComplete(Subtask<Boolean> subtask) {
 71                 boolean cancel = subtask.get();
 72                 return cancel;
 73             }
 74             @Override
 75             public Void result() {
 76                 return null;
 77             }
 78         };
 79 
 80         try (var scope = StructuredTaskScope.open(joiner, cf -> cf.withThreadFactory(factory))) {
 81             // fork subtasks
 82             for (int i = 0; i < beforeCancel; i++) {
 83                 scope.fork(() -> {
 84                     Thread.sleep(Duration.ofDays(1));
 85                     return false;
 86                 });
 87             }
 88 
 89             // fork subtask to cancel
 90             scope.fork(() -> true);
< prev index next >