< prev index next >

src/jdk.management/share/classes/com/sun/management/internal/VirtualThreadSchedulerImpls.java

Print this page
*** 22,33 ***
   * 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.
   */
  public class VirtualThreadSchedulerImpls {
      private VirtualThreadSchedulerImpls() {
      }
  
      public static VirtualThreadSchedulerMXBean create() {
!         if (ContinuationSupport.isSupported()) {
!             return new VirtualThreadSchedulerImpl();
-         } else {
              return new BoundVirtualThreadSchedulerImpl();
          }
      }
  
      /**
       * Base implementation of VirtualThreadSchedulerMXBean.
       */
--- 22,49 ---
   * or visit www.oracle.com if you need additional information or have any
   * questions.
   */
  package com.sun.management.internal;
  
  import java.util.concurrent.ForkJoinPool;
+ import java.util.concurrent.ThreadPoolExecutor;
  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 virtual thread scheduler.
   */
  public class VirtualThreadSchedulerImpls {
+     private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
+ 
      private VirtualThreadSchedulerImpls() {
      }
  
+     /**
+      * Creates the VirtualThreadSchedulerMXBean.
+      */
      public static VirtualThreadSchedulerMXBean create() {
!         // -XX:-VMContinuations
!         if (!ContinuationSupport.isSupported()) {
              return new BoundVirtualThreadSchedulerImpl();
          }
+ 
+         // built-in 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.
       */

*** 81,76 ***
          }
      }
  
      /**
       * Implementation of VirtualThreadSchedulerMXBean when virtual threads are
!      * implemented with continuations + 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;
-             }
          }
  
          @Override
          public int getParallelism() {
!             if (Scheduler.instance() instanceof ForkJoinPool pool) {
!                 return pool.getParallelism();
!             }
!             throw new InternalError();  // should not get here
          }
  
          @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
          }
  
          @Override
          public int getPoolSize() {
!             if (Scheduler.instance() instanceof ForkJoinPool pool) {
!                 return pool.getPoolSize();
!             }
!             return -1;  // should not get here
          }
  
          @Override
          public int getMountedVirtualThreadCount() {
!             if (Scheduler.instance() instanceof ForkJoinPool pool) {
!                 return pool.getActiveThreadCount();
!             }
!             return -1;  // should not get here
          }
  
          @Override
          public long getQueuedVirtualThreadCount() {
!             if (Scheduler.instance() instanceof ForkJoinPool pool) {
!                 return pool.getQueuedTaskCount() + pool.getQueuedSubmissionCount();
!             }
!             return -1L;  // should not get here
          }
      }
  
      /**
       * Implementation of VirtualThreadSchedulerMXBean when virtual threads are backed
       * by platform threads.
       */
!     private static final class BoundVirtualThreadSchedulerImpl extends BaseVirtualThreadSchedulerImpl {
          @Override
          public int getParallelism() {
              return Integer.MAX_VALUE;
          }
  
--- 97,71 ---
          }
      }
  
      /**
       * Implementation of VirtualThreadSchedulerMXBean when virtual threads are
!      * implemented with continuations and the built-in scheduler.
       */
!     private static final class BuiltinVirtualThreadSchedulerImpl
!             extends BaseVirtualThreadSchedulerImpl {
! 
!         private Thread.VirtualThreadScheduler builtinScheduler() {
!             return JLA.builtinVirtualThreadScheduler();
          }
  
          @Override
          public int getParallelism() {
!             return switch (builtinScheduler()) {
!                 case ForkJoinPool pool -> pool.getParallelism();
!                 case ThreadPoolExecutor pool -> pool.getMaximumPoolSize();
!                 default -> -1;
+             };
          }
  
          @Override
          public void setParallelism(int size) {
!             if (builtinScheduler() instanceof ForkJoinPool pool) {
                  pool.setParallelism(size);
!             } else {
!                 throw new UnsupportedOperationException();
              }
          }
  
          @Override
          public int getPoolSize() {
!             return switch (builtinScheduler()) {
!                 case ForkJoinPool pool -> pool.getPoolSize();
!                 case ThreadPoolExecutor pool -> pool.getPoolSize();
!                 default -> -1;
+             };
          }
  
          @Override
          public int getMountedVirtualThreadCount() {
!             return switch (builtinScheduler()) {
!                 case ForkJoinPool pool -> pool.getActiveThreadCount();
!                 case ThreadPoolExecutor pool -> pool.getActiveCount();
!                 default -> -1;
+             };
          }
  
          @Override
          public long getQueuedVirtualThreadCount() {
!             return switch (builtinScheduler()) {
!                 case ForkJoinPool pool -> pool.getQueuedTaskCount() + pool.getQueuedSubmissionCount();
!                 case ThreadPoolExecutor pool -> pool.getQueue().size();
!                 default -> -1L;
+             };
          }
      }
  
      /**
       * Implementation of VirtualThreadSchedulerMXBean when virtual threads are backed
       * by platform threads.
       */
!     private static final class BoundVirtualThreadSchedulerImpl
+             extends BaseVirtualThreadSchedulerImpl {
          @Override
          public int getParallelism() {
              return Integer.MAX_VALUE;
          }
  

*** 172,7 ***
--- 183,40 ---
          @Override
          public long getQueuedVirtualThreadCount() {
              return -1L;
          }
      }
+ 
+     /**
+      * Implementation of VirtualThreadSchedulerMXBean when using a custom virtual
+      * thread scheduler that does not implement VirtualThreadSchedulerMXBean.
+      */
+     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;
+         }
+     }
  }
  
< prev index next >