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  * @requires vm.continuations
27  * @requires test.thread.factory == null
28  * @run main/othervm Interference
29  */
30 
31 import java.lang.management.ManagementFactory;
32 import java.util.concurrent.Callable;
33 import java.util.concurrent.Executors;
34 import java.util.function.IntPredicate;
35 import java.util.stream.IntStream;
36 import jdk.management.VirtualThreadSchedulerMXBean;
37 
38 public class Interference {
39 
40     private enum Mode { SLEEP, SPIN };
41     private static volatile boolean done;
42     private static volatile Mode mode = Mode.SLEEP;
43 
44     public static void main(String[] args) throws Exception {
45         int parallelism = Runtime.getRuntime().availableProcessors();
46         if (parallelism < 2) {
47             return;
48         }
49 
50         Callable<?> task = () -> {
51             while (!done) {
52                 switch (mode) {
53                     case SPIN  -> Thread.onSpinWait();
54                     case SLEEP -> Thread.sleep(10);
55                 }
56             }
57             return null;
58         };
59 
60         try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
61             try {
62                 IntStream.range(0, parallelism).forEach(_ -> executor.submit(task));
63 
64                 // oscillate between sleeping and spinning
65                 for (int i = 1; i <= 10; i++) {
66                     System.out.println("--- iteration " + i + " ---");
67 
68                     mode = Mode.SLEEP;
69                     System.out.println("await mounted < " + parallelism);
70                     awaitMounted(n -> n < parallelism);
71 
72                     mode = Mode.SPIN;
73                     System.out.println("await mounted >= " + parallelism);
74                     awaitMounted(n -> n >= parallelism);
75                 }
76             } finally {
77                 done = true;
78             }
79         }
80     }
81 
82     static void awaitMounted(IntPredicate predicate) throws InterruptedException {
83         var bean = ManagementFactory.getPlatformMXBean(VirtualThreadSchedulerMXBean.class);
84         int attempts = 0;
85         for (;;) {
86             if (predicate.test(bean.getMountedVirtualThreadCount()))
87                 return;
88             System.out.println(bean);
89             if (++attempts > 20)
90                 throw new RuntimeException("Gave up");
91             Thread.sleep(500);
92         }
93     }
94 }