< prev index next > src/jdk.management/share/classes/com/sun/management/internal/VirtualThreadSchedulerImpls.java
Print this page
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.management.internal;
- import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;
import javax.management.ObjectName;
import jdk.management.VirtualThreadSchedulerMXBean;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.vm.ContinuationSupport;
import sun.management.Util;
/**
- * Provides the implementation of the management interface for the JDK's default virtual
- * thread scheduler.
+ * Provides the implementation of the management interface for the JDK's virtual thread scheduler.
*/
public class VirtualThreadSchedulerImpls {
+ private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
+
private VirtualThreadSchedulerImpls() {
}
+ /**
+ * Creates the VirtualThreadSchedulerMXBean.
+ */
public static VirtualThreadSchedulerMXBean create() {
- if (ContinuationSupport.isSupported()) {
- return new VirtualThreadSchedulerImpl();
- } else {
+ // -XX:-VMContinuations
+ if (!ContinuationSupport.isSupported()) {
return new BoundVirtualThreadSchedulerImpl();
}
+
+ // built-in ForkJoinPool scheduler
+ if (System.getProperty("jdk.virtualThreadScheduler.implClass") == null) {
+ return new BuiltinVirtualThreadSchedulerImpl();
+ }
+
+ // custom scheduler implements VirtualThreadSchedulerMXBean
+ if (JLA.defaultVirtualThreadScheduler() instanceof VirtualThreadSchedulerMXBean bean) {
+ return bean;
+ }
+
+ // custom scheduler does not implement VirtualThreadSchedulerMXBean
+ return new CustomVirtualThreadSchedulerImpl();
}
/**
* Base implementation of VirtualThreadSchedulerMXBean.
*/
}
}
/**
* Implementation of VirtualThreadSchedulerMXBean when virtual threads are
- * implemented with continuations + scheduler.
+ * implemented with continuations and the built-in ForkJoinPool scheduler.
*/
- private static final class VirtualThreadSchedulerImpl extends BaseVirtualThreadSchedulerImpl {
- /**
- * Holder class for scheduler.
- */
- private static class Scheduler {
- private static final Executor scheduler =
- SharedSecrets.getJavaLangAccess().virtualThreadDefaultScheduler();
- static Executor instance() {
- return scheduler;
- }
+ private static final class BuiltinVirtualThreadSchedulerImpl
+ extends BaseVirtualThreadSchedulerImpl {
+
+ private ForkJoinPool forkJoinPool() {
+ return (ForkJoinPool) JLA.builtinVirtualThreadScheduler();
}
@Override
public int getParallelism() {
- if (Scheduler.instance() instanceof ForkJoinPool pool) {
- return pool.getParallelism();
- }
- throw new InternalError(); // should not get here
+ return forkJoinPool().getParallelism();
}
@Override
public void setParallelism(int size) {
- if (Scheduler.instance() instanceof ForkJoinPool pool) {
- pool.setParallelism(size);
- if (pool.getPoolSize() < size) {
- // FJ worker thread creation is on-demand
- Thread.startVirtualThread(() -> { });
- }
-
- return;
- }
- throw new UnsupportedOperationException(); // should not get here
+ forkJoinPool().setParallelism(size);
}
@Override
public int getPoolSize() {
- if (Scheduler.instance() instanceof ForkJoinPool pool) {
- return pool.getPoolSize();
- }
- return -1; // should not get here
+ return forkJoinPool().getPoolSize();
}
@Override
public int getMountedVirtualThreadCount() {
- if (Scheduler.instance() instanceof ForkJoinPool pool) {
- return pool.getActiveThreadCount();
- }
- return -1; // should not get here
+ return forkJoinPool().getActiveThreadCount();
}
@Override
public long getQueuedVirtualThreadCount() {
- if (Scheduler.instance() instanceof ForkJoinPool pool) {
- return pool.getQueuedTaskCount() + pool.getQueuedSubmissionCount();
- }
- return -1L; // should not get here
+ ForkJoinPool p = forkJoinPool();
+ return p.getQueuedTaskCount() + p.getQueuedSubmissionCount();
+ }
+ }
+
+ /**
+ * Implementation of VirtualThreadSchedulerMXBean then a custom virtual thread scheduler
+ * is configured without a VirtualThreadSchedulerMXBean implementation.
+ */
+ private static final class CustomVirtualThreadSchedulerImpl
+ extends BaseVirtualThreadSchedulerImpl {
+
+ @Override
+ public int getParallelism() {
+ return 1;
+ }
+
+ @Override
+ public void setParallelism(int size) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int getPoolSize() {
+ return -1;
+ }
+
+ @Override
+ public int getMountedVirtualThreadCount() {
+ return -1;
+ }
+
+ @Override
+ public long getQueuedVirtualThreadCount() {
+ return -1L;
}
}
/**
* Implementation of VirtualThreadSchedulerMXBean when virtual threads are backed
< prev index next >