< prev index next >

src/java.base/share/classes/java/lang/ThreadBuilders.java

Print this page
@@ -29,14 +29,15 @@
  import java.lang.Thread.UncaughtExceptionHandler;
  import java.lang.invoke.MethodHandles;
  import java.lang.invoke.VarHandle;
  import java.util.Locale;
  import java.util.Objects;
- import java.util.concurrent.Executor;
  import java.util.concurrent.ThreadFactory;
  import jdk.internal.misc.Unsafe;
  import jdk.internal.invoke.MhUtil;
+ import jdk.internal.reflect.CallerSensitive;
+ import jdk.internal.reflect.Reflection;
  import jdk.internal.vm.ContinuationSupport;
  
  /**
   * Defines static methods to create platform and virtual thread builders.
   */

@@ -200,30 +201,22 @@
          @Override
          public ThreadFactory factory() {
              return new PlatformThreadFactory(group, name(), counter(), characteristics(),
                      daemonChanged, daemon, priority, stackSize, uncaughtExceptionHandler());
          }
- 
      }
  
      /**
       * ThreadBuilder.OfVirtual implementation.
       */
      static final class VirtualThreadBuilder
              extends BaseThreadBuilder implements OfVirtual {
-         private Executor scheduler;
+         private Thread.VirtualThreadScheduler scheduler;
  
          VirtualThreadBuilder() {
          }
  
-         // invoked by tests
-         VirtualThreadBuilder(Executor scheduler) {
-             if (!ContinuationSupport.isSupported())
-                 throw new UnsupportedOperationException();
-             this.scheduler = Objects.requireNonNull(scheduler);
-         }
- 
          @Override
          public OfVirtual name(String name) {
              setName(name);
              return this;
          }

@@ -247,11 +240,16 @@
          }
  
          @Override
          public Thread unstarted(Runnable task) {
              Objects.requireNonNull(task);
-             var thread = newVirtualThread(scheduler, nextThreadName(), characteristics(), task);
+             var thread = newVirtualThread(scheduler,
+                                           null,
+                                           nextThreadName(),
+                                           characteristics(),
+                                           task,
+                                           null);
              UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
              if (uhe != null)
                  thread.uncaughtExceptionHandler(uhe);
              return thread;
          }

@@ -266,10 +264,45 @@
          @Override
          public ThreadFactory factory() {
              return new VirtualThreadFactory(scheduler, name(), counter(), characteristics(),
                      uncaughtExceptionHandler());
          }
+ 
+         @CallerSensitive
+         @Override
+         public Thread unstarted(Runnable task, Thread preferredCarrier, Object att) {
+             Objects.requireNonNull(task);
+             Class<?> caller = Reflection.getCallerClass();
+             caller.getModule().ensureNativeAccess(OfVirtual.class, "unstarted", caller, false);
+             var thread = newVirtualThread(scheduler,
+                                           preferredCarrier,
+                                           nextThreadName(),
+                                           characteristics(),
+                                           task,
+                                           att);
+             UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
+             if (uhe != null)
+                 thread.uncaughtExceptionHandler(uhe);
+             return thread;
+         }
+ 
+         @CallerSensitive
+         @Override
+         public OfVirtual scheduler(Thread.VirtualThreadScheduler scheduler) {
+             if (!ContinuationSupport.isSupported()) {
+                 throw new UnsupportedOperationException();
+             }
+             // can't mix custom default scheduler and API prototypes at this time
+             if (scheduler != VirtualThread.defaultScheduler()
+                     && VirtualThread.defaultScheduler() != VirtualThread.builtinScheduler()) {
+                 throw new UnsupportedOperationException();
+             }
+             Class<?> caller = Reflection.getCallerClass();
+             caller.getModule().ensureNativeAccess(OfVirtual.class, "scheduler", caller, false);
+             this.scheduler = Objects.requireNonNull(scheduler);
+             return this;
+         }
      }
  
      /**
       * Base ThreadFactory implementation.
       */

@@ -367,13 +400,13 @@
  
      /**
       * ThreadFactory for virtual threads.
       */
      private static class VirtualThreadFactory extends BaseThreadFactory {
-         private final Executor scheduler;
+         private final Thread.VirtualThreadScheduler scheduler;
  
-         VirtualThreadFactory(Executor scheduler,
+         VirtualThreadFactory(Thread.VirtualThreadScheduler scheduler,
                               String name,
                               long start,
                               int characteristics,
                               UncaughtExceptionHandler uhe) {
              super(name, start, characteristics, uhe);

@@ -382,34 +415,40 @@
  
          @Override
          public Thread newThread(Runnable task) {
              Objects.requireNonNull(task);
              String name = nextThreadName();
-             Thread thread = newVirtualThread(scheduler, name, characteristics(), task);
+             Thread thread = newVirtualThread(scheduler, null, name, characteristics(), task, null);
              UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
              if (uhe != null)
                  thread.uncaughtExceptionHandler(uhe);
              return thread;
          }
      }
  
      /**
       * Creates a new virtual thread to run the given task.
       */
-     static Thread newVirtualThread(Executor scheduler,
-                                    String name,
-                                    int characteristics,
-                                    Runnable task) {
+     private static Thread newVirtualThread(Thread.VirtualThreadScheduler scheduler,
+                                            Thread preferredCarrier,
+                                            String name,
+                                            int characteristics,
+                                            Runnable task,
+                                            Object att) {
          if (ContinuationSupport.isSupported()) {
-             return new VirtualThread(scheduler, name, characteristics, task);
+             return new VirtualThread(scheduler, preferredCarrier, name, characteristics, task, att);
          } else {
              if (scheduler != null)
                  throw new UnsupportedOperationException();
              return new BoundVirtualThread(name, characteristics, task);
          }
      }
  
+     static Thread newVirtualThread(String name, int characteristics, Runnable task) {
+        return newVirtualThread(null, null, name, characteristics, task, null);
+     }
+ 
      /**
       * A "virtual thread" that is backed by a platform thread. This implementation
       * is intended for platforms that don't have the underlying VM support for
       * continuations. It can also be used for testing.
       */
< prev index next >