1 /*
 2  * Copyright (c) 2026, 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 package org.openjdk.bench.java.util.concurrent.forkjoin;
24 
25 import java.util.*;
26 import java.util.concurrent.*;
27 import org.openjdk.jmh.annotations.*;
28 
29 /**
30  * One thread scheduling + cancelling delayed tasks. CPU optionally saturated.
31  */
32 
33 @BenchmarkMode(Mode.AverageTime)
34 @OutputTimeUnit(TimeUnit.NANOSECONDS)
35 @State(Scope.Benchmark)
36 @Warmup(iterations = 5, time = 1)
37 @Measurement(iterations = 5, time = 1)
38 @Fork(3)
39 public class ScheduleAndCancelDelayedTasks1 {
40     private ForkJoinPool fjpPool;
41     private ExecutorService threadPool;
42     private volatile boolean done;
43     private Deque<Future<?>> pendingDelayedTasks;
44 
45     private void spinUntilDone() {
46         while (!done) {
47             Thread.onSpinWait();
48         }
49     }
50 
51     @Param({"true", "false"})
52     boolean cpuSaturated;
53 
54     @Param({"1", "100"})
55     int maxPending;
56 
57     @Setup
58     public void setup() {
59         int ncores = Runtime.getRuntime().availableProcessors();
60         fjpPool = new ForkJoinPool(ncores);
61         fjpPool.cancelDelayedTasksOnShutdown();
62 
63         // saturate CPU
64         if (cpuSaturated) {
65             threadPool = Executors.newCachedThreadPool();
66             for (int i = 0; i < (ncores - 1); i++) {
67                 threadPool.submit(this::spinUntilDone);
68             }
69         }
70 
71         pendingDelayedTasks = new ArrayDeque<>();
72     }
73 
74     @TearDown
75     public void teardown() {
76         done = true;
77         if (threadPool != null) threadPool.close();
78         fjpPool.close();
79     }
80 
81     @Benchmark
82     public void test() throws Exception {
83         long delay = 60 + ThreadLocalRandom.current().nextInt(60);
84         Future<?> future = fjpPool.schedule(() -> { }, delay, TimeUnit.SECONDS);
85         if (maxPending > 1) {
86             pendingDelayedTasks.offer(future);
87             if (pendingDelayedTasks.size() >= maxPending) {
88                 future = pendingDelayedTasks.poll();
89             }
90         }
91         future.cancel(false);
92     }
93 }