1 /*
 2  * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @summary Test ForkJoinPool  signalling with a virtual thread workload that oscillates
27  *     between busy and idle
28  * @requires vm.continuations
29  * @requires test.thread.factory == null
30  * @run main/othervm ${test.main.class}
31  */
32 
33 import java.lang.management.ManagementFactory;
34 import java.util.concurrent.Callable;
35 import java.util.concurrent.Executors;
36 import java.util.function.IntPredicate;
37 import java.util.stream.IntStream;
38 import jdk.management.VirtualThreadSchedulerMXBean;
39 
40 public class UpDownLoad {
41 
42     private enum Mode { SLEEP, SPIN };
43     private static volatile boolean done;
44     private static volatile Mode mode = Mode.SLEEP;
45 
46     public static void main(String[] args) throws Exception {
47         int parallelism = Runtime.getRuntime().availableProcessors();
48         if (parallelism < 2) {
49             return;
50         }
51 
52         Callable<?> task = () -> {
53             while (!done) {
54                 switch (mode) {
55                     case SPIN  -> Thread.onSpinWait();
56                     case SLEEP -> Thread.sleep(10);
57                 }
58             }
59             return null;
60         };
61 
62         try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
63             try {
64                 IntStream.range(0, parallelism).forEach(_ -> executor.submit(task));
65 
66                 // oscillate between sleeping and spinning
67                 for (int i = 1; i <= 10; i++) {
68                     System.out.println("--- iteration " + i + " ---");
69 
70                     mode = Mode.SLEEP;
71                     System.out.println("await mounted < " + parallelism);
72                     awaitMounted(n -> n < parallelism);
73 
74                     mode = Mode.SPIN;
75                     System.out.println("await mounted >= " + parallelism);
76                     awaitMounted(n -> n >= parallelism);
77                 }
78             } finally {
79                 done = true;
80             }
81         }
82     }
83 
84     static void awaitMounted(IntPredicate predicate) throws InterruptedException {
85         var bean = ManagementFactory.getPlatformMXBean(VirtualThreadSchedulerMXBean.class);
86         int attempts = 0;
87         for (;;) {
88             if (predicate.test(bean.getMountedVirtualThreadCount()))
89                 return;
90             System.out.println(bean);
91             if (++attempts > 20)
92                 throw new RuntimeException("Gave up");
93             Thread.sleep(500);
94         }
95     }
96 }