< prev index next >

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

Print this page

   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.lang;
  26 

  27 import java.util.Locale;
  28 import java.util.Objects;
  29 import java.util.concurrent.CountDownLatch;
  30 import java.util.concurrent.Executor;
  31 import java.util.concurrent.Executors;
  32 import java.util.concurrent.ForkJoinPool;
  33 import java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory;
  34 import java.util.concurrent.ForkJoinTask;
  35 import java.util.concurrent.Future;
  36 import java.util.concurrent.RejectedExecutionException;
  37 import java.util.concurrent.ScheduledExecutorService;
  38 import java.util.concurrent.ScheduledThreadPoolExecutor;
  39 import java.util.concurrent.TimeUnit;
  40 import jdk.internal.event.VirtualThreadEndEvent;
  41 import jdk.internal.event.VirtualThreadStartEvent;
  42 import jdk.internal.event.VirtualThreadSubmitFailedEvent;
  43 import jdk.internal.misc.CarrierThread;
  44 import jdk.internal.misc.InnocuousThread;
  45 import jdk.internal.misc.Unsafe;
  46 import jdk.internal.vm.Continuation;
  47 import jdk.internal.vm.ContinuationScope;
  48 import jdk.internal.vm.StackableScope;
  49 import jdk.internal.vm.ThreadContainer;
  50 import jdk.internal.vm.ThreadContainers;
  51 import jdk.internal.vm.annotation.ChangesCurrentThread;
  52 import jdk.internal.vm.annotation.Hidden;
  53 import jdk.internal.vm.annotation.IntrinsicCandidate;
  54 import jdk.internal.vm.annotation.JvmtiHideEvents;
  55 import jdk.internal.vm.annotation.JvmtiMountTransition;
  56 import jdk.internal.vm.annotation.ReservedStackAccess;
  57 import sun.nio.ch.Interruptible;
  58 import static java.util.concurrent.TimeUnit.*;
  59 
  60 /**
  61  * A thread that is scheduled by the Java virtual machine rather than the operating system.
  62  */
  63 final class VirtualThread extends BaseVirtualThread {
  64     private static final Unsafe U = Unsafe.getUnsafe();
  65     private static final ContinuationScope VTHREAD_SCOPE = new ContinuationScope("VirtualThreads");
  66     private static final ForkJoinPool DEFAULT_SCHEDULER = createDefaultScheduler();













  67 
  68     private static final long STATE = U.objectFieldOffset(VirtualThread.class, "state");
  69     private static final long PARK_PERMIT = U.objectFieldOffset(VirtualThread.class, "parkPermit");
  70     private static final long CARRIER_THREAD = U.objectFieldOffset(VirtualThread.class, "carrierThread");
  71     private static final long TERMINATION = U.objectFieldOffset(VirtualThread.class, "termination");
  72     private static final long ON_WAITING_LIST = U.objectFieldOffset(VirtualThread.class, "onWaitingList");
  73 
  74     // scheduler and continuation
  75     private final Executor scheduler;
  76     private final Continuation cont;
  77     private final Runnable runContinuation;
  78 
  79     // virtual thread state, accessed by VM
  80     private volatile int state;
  81 
  82     /*
  83      * Virtual thread state transitions:
  84      *
  85      *      NEW -> STARTED         // Thread.start, schedule to run
  86      *  STARTED -> TERMINATED      // failed to start
  87      *  STARTED -> RUNNING         // first run
  88      *  RUNNING -> TERMINATED      // done
  89      *
  90      *  RUNNING -> PARKING         // Thread parking with LockSupport.park
  91      *  PARKING -> PARKED          // cont.yield successful, parked indefinitely
  92      *  PARKING -> PINNED          // cont.yield failed, parked indefinitely on carrier
  93      *   PARKED -> UNPARKED        // unparked, may be scheduled to continue
  94      *   PINNED -> RUNNING         // unparked, continue execution on same carrier
  95      * UNPARKED -> RUNNING         // continue execution after park

 151     private static final int TERMINATED = 99;  // final state
 152 
 153     // can be suspended from scheduling when unmounted
 154     private static final int SUSPENDED = 1 << 8;
 155 
 156     // parking permit made available by LockSupport.unpark
 157     private volatile boolean parkPermit;
 158 
 159     // blocking permit made available by unblocker thread when another thread exits monitor
 160     private volatile boolean blockPermit;
 161 
 162     // true when on the list of virtual threads waiting to be unblocked
 163     private volatile boolean onWaitingList;
 164 
 165     // next virtual thread on the list of virtual threads waiting to be unblocked
 166     private volatile VirtualThread next;
 167 
 168     // notified by Object.notify/notifyAll while waiting in Object.wait
 169     private volatile boolean notified;
 170 



 171     // timed-wait support
 172     private byte timedWaitSeqNo;
 173 
 174     // timeout for timed-park and timed-wait, only accessed on current/carrier thread
 175     private long timeout;
 176 
 177     // timer task for timed-park and timed-wait, only accessed on current/carrier thread
 178     private Future<?> timeoutTask;
 179 
 180     // carrier thread when mounted, accessed by VM
 181     private volatile Thread carrierThread;
 182 
 183     // termination object when joining, created lazily if needed
 184     private volatile CountDownLatch termination;
 185 
 186     /**
 187      * Returns the default scheduler.
 188      */
 189     static Executor defaultScheduler() {
 190         return DEFAULT_SCHEDULER;
 191     }
 192 







 193     /**
 194      * Returns the continuation scope used for virtual threads.
 195      */
 196     static ContinuationScope continuationScope() {
 197         return VTHREAD_SCOPE;
 198     }
 199 
 200     /**
 201      * Creates a new {@code VirtualThread} to run the given task with the given
 202      * scheduler. If the given scheduler is {@code null} and the current thread
 203      * is a platform thread then the newly created virtual thread will use the
 204      * default scheduler. If given scheduler is {@code null} and the current
 205      * thread is a virtual thread then the current thread's scheduler is used.








 206      *
 207      * @param scheduler the scheduler or null
 208      * @param name thread name
 209      * @param characteristics characteristics
 210      * @param task the task to execute
 211      */
 212     VirtualThread(Executor scheduler, String name, int characteristics, Runnable task) {



 213         super(name, characteristics, /*bound*/ false);
 214         Objects.requireNonNull(task);
 215 
 216         // choose scheduler if not specified
 217         if (scheduler == null) {
 218             Thread parent = Thread.currentThread();
 219             if (parent instanceof VirtualThread vparent) {
 220                 scheduler = vparent.scheduler;
 221             } else {
 222                 scheduler = DEFAULT_SCHEDULER;
 223             }
 224         }
 225 
 226         this.scheduler = scheduler;
 227         this.cont = new VThreadContinuation(this, task);
 228         this.runContinuation = this::runContinuation;
 229     }
 230 
 231     /**
 232      * The continuation that a virtual thread executes.
 233      */
 234     private static class VThreadContinuation extends Continuation {
 235         VThreadContinuation(VirtualThread vthread, Runnable task) {
 236             super(VTHREAD_SCOPE, wrap(vthread, task));
 237         }
 238         @Override
 239         protected void onPinned(Continuation.Pinned reason) {
 240         }
 241         private static Runnable wrap(VirtualThread vthread, Runnable task) {
 242             return new Runnable() {
 243                 @Hidden

 299                 afterYield();
 300             }
 301         }
 302     }
 303 
 304     /**
 305      * Cancel timeout task when continuing after timed-park or timed-wait.
 306      * The timeout task may be executing, or may have already completed.
 307      */
 308     private void cancelTimeoutTask() {
 309         if (timeoutTask != null) {
 310             timeoutTask.cancel(false);
 311             timeoutTask = null;
 312         }
 313     }
 314 
 315     /**
 316      * Submits the runContinuation task to the scheduler. For the default scheduler,
 317      * and calling it on a worker thread, the task will be pushed to the local queue,
 318      * otherwise it will be pushed to an external submission queue.
 319      * @param scheduler the scheduler
 320      * @param retryOnOOME true to retry indefinitely if OutOfMemoryError is thrown
 321      * @throws RejectedExecutionException
 322      */
 323     private void submitRunContinuation(Executor scheduler, boolean retryOnOOME) {
 324         boolean done = false;
 325         while (!done) {
 326             try {
 327                 // Pin the continuation to prevent the virtual thread from unmounting
 328                 // when submitting a task. For the default scheduler this ensures that
 329                 // the carrier doesn't change when pushing a task. For other schedulers
 330                 // it avoids deadlock that could arise due to carriers and virtual
 331                 // threads contending for a lock.
 332                 if (currentThread().isVirtual()) {
 333                     Continuation.pin();
 334                     try {
 335                         scheduler.execute(runContinuation);
 336                     } finally {
 337                         Continuation.unpin();
 338                     }
 339                 } else {
 340                     scheduler.execute(runContinuation);
 341                 }
 342                 done = true;
 343             } catch (RejectedExecutionException ree) {
 344                 submitFailed(ree);
 345                 throw ree;
 346             } catch (OutOfMemoryError e) {
 347                 if (retryOnOOME) {
 348                     U.park(false, 100_000_000); // 100ms
 349                 } else {
 350                     throw e;
 351                 }
 352             }
 353         }
 354     }
 355 
 356     /**
 357      * Submits the runContinuation task to the given scheduler as an external submit.
 358      * If OutOfMemoryError is thrown then the submit will be retried until it succeeds.
 359      * @throws RejectedExecutionException
 360      * @see ForkJoinPool#externalSubmit(ForkJoinTask)
 361      */
 362     private void externalSubmitRunContinuation(ForkJoinPool pool) {
 363         assert Thread.currentThread() instanceof CarrierThread;
 364         try {
 365             pool.externalSubmit(ForkJoinTask.adapt(runContinuation));
 366         } catch (RejectedExecutionException ree) {
 367             submitFailed(ree);
 368             throw ree;
 369         } catch (OutOfMemoryError e) {
 370             submitRunContinuation(pool, true);
 371         }
 372     }
 373 
 374     /**
 375      * Submits the runContinuation task to the scheduler. For the default scheduler,
 376      * and calling it on a worker thread, the task will be pushed to the local queue,
 377      * otherwise it will be pushed to an external submission queue.
 378      * If OutOfMemoryError is thrown then the submit will be retried until it succeeds.
 379      * @throws RejectedExecutionException
 380      */
 381     private void submitRunContinuation() {
 382         submitRunContinuation(scheduler, true);
 383     }
 384 
 385     /**
 386      * Lazy submit the runContinuation task if invoked on a carrier thread and its local
 387      * queue is empty. If not empty, or invoked by another thread, then this method works
 388      * like submitRunContinuation and just submits the task to the scheduler.
 389      * If OutOfMemoryError is thrown then the submit will be retried until it succeeds.
 390      * @throws RejectedExecutionException
 391      * @see ForkJoinPool#lazySubmit(ForkJoinTask)
 392      */
 393     private void lazySubmitRunContinuation() {
 394         if (currentThread() instanceof CarrierThread ct && ct.getQueuedTaskCount() == 0) {
 395             ForkJoinPool pool = ct.getPool();

 396             try {
 397                 pool.lazySubmit(ForkJoinTask.adapt(runContinuation));





















 398             } catch (RejectedExecutionException ree) {
 399                 submitFailed(ree);
 400                 throw ree;
 401             } catch (OutOfMemoryError e) {
 402                 submitRunContinuation();
 403             }
 404         } else {
 405             submitRunContinuation();
 406         }
 407     }
 408 
 409     /**
 410      * Submits the runContinuation task to the scheduler. For the default scheduler, and
 411      * calling it a virtual thread that uses the default scheduler, the task will be
 412      * pushed to an external submission queue. This method may throw OutOfMemoryError.
 413      * @throws RejectedExecutionException
 414      * @throws OutOfMemoryError
 415      */
 416     private void externalSubmitRunContinuationOrThrow() {
 417         if (scheduler == DEFAULT_SCHEDULER && currentCarrierThread() instanceof CarrierThread ct) {
 418             try {
 419                 ct.getPool().externalSubmit(ForkJoinTask.adapt(runContinuation));
 420             } catch (RejectedExecutionException ree) {
 421                 submitFailed(ree);
 422                 throw ree;
 423             }
 424         } else {
 425             submitRunContinuation(scheduler, false);
 426         }
 427     }
 428 
 429     /**
 430      * If enabled, emits a JFR VirtualThreadSubmitFailedEvent.
 431      */
 432     private void submitFailed(RejectedExecutionException ree) {
 433         var event = new VirtualThreadSubmitFailedEvent();
 434         if (event.isEnabled()) {
 435             event.javaThreadId = threadId();
 436             event.exceptionMessage = ree.getMessage();
 437             event.commit();
 438         }
 439     }
 440 
 441     /**
 442      * Runs a task in the context of this virtual thread.
 443      */
 444     private void run(Runnable task) {
 445         assert Thread.currentThread() == this && state == RUNNING;

 560                 long timeout = this.timeout;
 561                 assert timeout > 0;
 562                 timeoutTask = schedule(this::parkTimeoutExpired, timeout, NANOSECONDS);
 563                 setState(newState = TIMED_PARKED);
 564             }
 565 
 566             // may have been unparked while parking
 567             if (parkPermit && compareAndSetState(newState, UNPARKED)) {
 568                 // lazy submit if local queue is empty
 569                 lazySubmitRunContinuation();
 570             }
 571             return;
 572         }
 573 
 574         // Thread.yield
 575         if (s == YIELDING) {
 576             setState(YIELDED);
 577 
 578             // external submit if there are no tasks in the local task queue
 579             if (currentThread() instanceof CarrierThread ct && ct.getQueuedTaskCount() == 0) {
 580                 externalSubmitRunContinuation(ct.getPool());
 581             } else {
 582                 submitRunContinuation();
 583             }
 584             return;
 585         }
 586 
 587         // blocking on monitorenter
 588         if (s == BLOCKING) {
 589             setState(BLOCKED);
 590 
 591             // may have been unblocked while blocking
 592             if (blockPermit && compareAndSetState(BLOCKED, UNBLOCKED)) {
 593                 // lazy submit if local queue is empty
 594                 lazySubmitRunContinuation();
 595             }
 596             return;
 597         }
 598 
 599         // Object.wait
 600         if (s == WAITING || s == TIMED_WAITING) {
 601             int newState;

 602             if (s == WAITING) {
 603                 setState(newState = WAIT);
 604             } else {
 605                 // For timed-wait, a timeout task is scheduled to execute. The timeout
 606                 // task will change the thread state to UNBLOCKED and submit the thread
 607                 // to the scheduler. A sequence number is used to ensure that the timeout
 608                 // task only unblocks the thread for this timed-wait. We synchronize with
 609                 // the timeout task to coordinate access to the sequence number and to
 610                 // ensure the timeout task doesn't execute until the thread has got to
 611                 // the TIMED_WAIT state.
 612                 long timeout = this.timeout;
 613                 assert timeout > 0;
 614                 synchronized (timedWaitLock()) {
 615                     byte seqNo = ++timedWaitSeqNo;
 616                     timeoutTask = schedule(() -> waitTimeoutExpired(seqNo), timeout, MILLISECONDS);
 617                     setState(newState = TIMED_WAIT);
 618                 }
 619             }
 620 
 621             // may have been notified while in transition to wait state
 622             if (notified && compareAndSetState(newState, BLOCKED)) {
 623                 // may have even been unblocked already
 624                 if (blockPermit && compareAndSetState(BLOCKED, UNBLOCKED)) {
 625                     submitRunContinuation();
 626                 }
 627                 return;
 628             }
 629 
 630             // may have been interrupted while in transition to wait state
 631             if (interrupted && compareAndSetState(newState, UNBLOCKED)) {
 632                 submitRunContinuation();
 633                 return;
 634             }
 635             return;
 636         }
 637 
 638         assert false;
 639     }
 640 
 641     /**
 642      * Invoked after the continuation completes.
 643      */
 644     private void afterDone() {
 645         afterDone(true);
 646     }
 647 
 648     /**
 649      * Invoked after the continuation completes (or start failed). Sets the thread
 650      * state to TERMINATED and notifies anyone waiting for the thread to terminate.
 651      *

1398     @IntrinsicCandidate
1399     @JvmtiMountTransition
1400     private native void notifyJvmtiMount(boolean hide);
1401 
1402     @IntrinsicCandidate
1403     @JvmtiMountTransition
1404     private native void notifyJvmtiUnmount(boolean hide);
1405 
1406     @IntrinsicCandidate
1407     private static native void notifyJvmtiDisableSuspend(boolean enter);
1408 
1409     private static native void registerNatives();
1410     static {
1411         registerNatives();
1412 
1413         // ensure VTHREAD_GROUP is created, may be accessed by JVMTI
1414         var group = Thread.virtualThreadGroup();
1415     }
1416 
1417     /**
1418      * Creates the default ForkJoinPool scheduler.



























1419      */
1420     private static ForkJoinPool createDefaultScheduler() {
1421         ForkJoinWorkerThreadFactory factory = pool -> new CarrierThread(pool);
1422         int parallelism, maxPoolSize, minRunnable;
1423         String parallelismValue = System.getProperty("jdk.virtualThreadScheduler.parallelism");
1424         String maxPoolSizeValue = System.getProperty("jdk.virtualThreadScheduler.maxPoolSize");
1425         String minRunnableValue = System.getProperty("jdk.virtualThreadScheduler.minRunnable");
1426         if (parallelismValue != null) {
1427             parallelism = Integer.parseInt(parallelismValue);
1428         } else {
1429             parallelism = Runtime.getRuntime().availableProcessors();
1430         }
1431         if (maxPoolSizeValue != null) {
1432             maxPoolSize = Integer.parseInt(maxPoolSizeValue);
1433             parallelism = Integer.min(parallelism, maxPoolSize);
1434         } else {
1435             maxPoolSize = Integer.max(parallelism, 256);
1436         }
1437         if (minRunnableValue != null) {
1438             minRunnable = Integer.parseInt(minRunnableValue);
1439         } else {
1440             minRunnable = Integer.max(parallelism / 2, 1);
1441         }
1442         Thread.UncaughtExceptionHandler handler = (t, e) -> { };
1443         boolean asyncMode = true; // FIFO
1444         return new ForkJoinPool(parallelism, factory, handler, asyncMode,
1445                      0, maxPoolSize, minRunnable, pool -> true, 30, SECONDS);












































1446     }
1447 
1448     /**
1449      * Schedule a runnable task to run after a delay.
1450      */
1451     private Future<?> schedule(Runnable command, long delay, TimeUnit unit) {
1452         if (scheduler instanceof ForkJoinPool pool) {
1453             return pool.schedule(command, delay, unit);
1454         } else {
1455             return DelayedTaskSchedulers.schedule(command, delay, unit);
1456         }
1457     }
1458 
1459     /**
1460      * Supports scheduling a runnable task to run after a delay. It uses a number
1461      * of ScheduledThreadPoolExecutor instances to reduce contention on the delayed
1462      * work queue used. This class is used when using a custom scheduler.
1463      */
1464     private static class DelayedTaskSchedulers {
1465         private static final ScheduledExecutorService[] INSTANCE = createDelayedTaskSchedulers();

1515                 assert changed;
1516                 vthread.unblock();
1517 
1518                 vthread = nextThread;
1519             }
1520         }
1521     }
1522 
1523     /**
1524      * Retrieves the list of virtual threads that are waiting to be unblocked, waiting
1525      * if necessary until a list of one or more threads becomes available.
1526      */
1527     private static native VirtualThread takeVirtualThreadListToUnblock();
1528 
1529     static {
1530         var unblocker = InnocuousThread.newThread("VirtualThread-unblocker",
1531                 VirtualThread::unblockVirtualThreads);
1532         unblocker.setDaemon(true);
1533         unblocker.start();
1534     }
1535 }

   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.lang;
  26 
  27 import java.lang.reflect.Constructor;
  28 import java.util.Locale;
  29 import java.util.Objects;
  30 import java.util.concurrent.CountDownLatch;

  31 import java.util.concurrent.Executors;
  32 import java.util.concurrent.ForkJoinPool;

  33 import java.util.concurrent.ForkJoinTask;
  34 import java.util.concurrent.Future;
  35 import java.util.concurrent.RejectedExecutionException;
  36 import java.util.concurrent.ScheduledExecutorService;
  37 import java.util.concurrent.ScheduledThreadPoolExecutor;
  38 import java.util.concurrent.TimeUnit;
  39 import jdk.internal.event.VirtualThreadEndEvent;
  40 import jdk.internal.event.VirtualThreadStartEvent;
  41 import jdk.internal.event.VirtualThreadSubmitFailedEvent;
  42 import jdk.internal.misc.CarrierThread;
  43 import jdk.internal.misc.InnocuousThread;
  44 import jdk.internal.misc.Unsafe;
  45 import jdk.internal.vm.Continuation;
  46 import jdk.internal.vm.ContinuationScope;
  47 import jdk.internal.vm.StackableScope;
  48 import jdk.internal.vm.ThreadContainer;
  49 import jdk.internal.vm.ThreadContainers;
  50 import jdk.internal.vm.annotation.ChangesCurrentThread;
  51 import jdk.internal.vm.annotation.Hidden;
  52 import jdk.internal.vm.annotation.IntrinsicCandidate;
  53 import jdk.internal.vm.annotation.JvmtiHideEvents;
  54 import jdk.internal.vm.annotation.JvmtiMountTransition;
  55 import jdk.internal.vm.annotation.ReservedStackAccess;
  56 import sun.nio.ch.Interruptible;
  57 import static java.util.concurrent.TimeUnit.*;
  58 
  59 /**
  60  * A thread that is scheduled by the Java virtual machine rather than the operating system.
  61  */
  62 final class VirtualThread extends BaseVirtualThread {
  63     private static final Unsafe U = Unsafe.getUnsafe();
  64     private static final ContinuationScope VTHREAD_SCOPE = new ContinuationScope("VirtualThreads");
  65 
  66     private static final VirtualThreadScheduler DEFAULT_SCHEDULER;
  67     private static final boolean IS_CUSTOM_DEFAULT_SCHEDULER;
  68     static {
  69         // experimental
  70         String propValue = System.getProperty("jdk.virtualThreadScheduler.implClass");
  71         if (propValue != null) {
  72             DEFAULT_SCHEDULER = createCustomDefaultScheduler(propValue);
  73             IS_CUSTOM_DEFAULT_SCHEDULER = true;
  74         } else {
  75             DEFAULT_SCHEDULER = createDefaultForkJoinPoolScheduler();
  76             IS_CUSTOM_DEFAULT_SCHEDULER = false;
  77         }
  78     }
  79 
  80     private static final long STATE = U.objectFieldOffset(VirtualThread.class, "state");
  81     private static final long PARK_PERMIT = U.objectFieldOffset(VirtualThread.class, "parkPermit");
  82     private static final long CARRIER_THREAD = U.objectFieldOffset(VirtualThread.class, "carrierThread");
  83     private static final long TERMINATION = U.objectFieldOffset(VirtualThread.class, "termination");
  84     private static final long ON_WAITING_LIST = U.objectFieldOffset(VirtualThread.class, "onWaitingList");
  85 
  86     // scheduler and continuation
  87     private final VirtualThreadScheduler scheduler;
  88     private final Continuation cont;
  89     private final Runnable runContinuation;
  90 
  91     // virtual thread state, accessed by VM
  92     private volatile int state;
  93 
  94     /*
  95      * Virtual thread state transitions:
  96      *
  97      *      NEW -> STARTED         // Thread.start, schedule to run
  98      *  STARTED -> TERMINATED      // failed to start
  99      *  STARTED -> RUNNING         // first run
 100      *  RUNNING -> TERMINATED      // done
 101      *
 102      *  RUNNING -> PARKING         // Thread parking with LockSupport.park
 103      *  PARKING -> PARKED          // cont.yield successful, parked indefinitely
 104      *  PARKING -> PINNED          // cont.yield failed, parked indefinitely on carrier
 105      *   PARKED -> UNPARKED        // unparked, may be scheduled to continue
 106      *   PINNED -> RUNNING         // unparked, continue execution on same carrier
 107      * UNPARKED -> RUNNING         // continue execution after park

 163     private static final int TERMINATED = 99;  // final state
 164 
 165     // can be suspended from scheduling when unmounted
 166     private static final int SUSPENDED = 1 << 8;
 167 
 168     // parking permit made available by LockSupport.unpark
 169     private volatile boolean parkPermit;
 170 
 171     // blocking permit made available by unblocker thread when another thread exits monitor
 172     private volatile boolean blockPermit;
 173 
 174     // true when on the list of virtual threads waiting to be unblocked
 175     private volatile boolean onWaitingList;
 176 
 177     // next virtual thread on the list of virtual threads waiting to be unblocked
 178     private volatile VirtualThread next;
 179 
 180     // notified by Object.notify/notifyAll while waiting in Object.wait
 181     private volatile boolean notified;
 182 
 183     // true when waiting in Object.wait, false for VM internal uninterruptible Object.wait
 184     private volatile boolean interruptableWait;
 185 
 186     // timed-wait support
 187     private byte timedWaitSeqNo;
 188 
 189     // timeout for timed-park and timed-wait, only accessed on current/carrier thread
 190     private long timeout;
 191 
 192     // timer task for timed-park and timed-wait, only accessed on current/carrier thread
 193     private Future<?> timeoutTask;
 194 
 195     // carrier thread when mounted, accessed by VM
 196     private volatile Thread carrierThread;
 197 
 198     // termination object when joining, created lazily if needed
 199     private volatile CountDownLatch termination;
 200 
 201     /**
 202      * Returns the default scheduler.
 203      */
 204     static VirtualThreadScheduler defaultScheduler() {
 205         return DEFAULT_SCHEDULER;
 206     }
 207 
 208     /**
 209      * Returns true if using a custom default scheduler.
 210      */
 211     static boolean isCustomDefaultScheduler() {
 212         return IS_CUSTOM_DEFAULT_SCHEDULER;
 213     }
 214 
 215     /**
 216      * Returns the continuation scope used for virtual threads.
 217      */
 218     static ContinuationScope continuationScope() {
 219         return VTHREAD_SCOPE;
 220     }
 221 
 222     /**
 223      * Return the scheduler for this thread.
 224      * @param revealBuiltin true to reveal the built-in default scheduler, false to hide
 225      */
 226     VirtualThreadScheduler scheduler(boolean revealBuiltin) {
 227         if (scheduler instanceof BuiltinDefaultScheduler builtin && !revealBuiltin) {
 228             return builtin.externalView();
 229         } else {
 230             return scheduler;
 231         }
 232     }
 233 
 234     /**
 235      * Creates a new {@code VirtualThread} to run the given task with the given scheduler.
 236      *
 237      * @param scheduler the scheduler or null for default scheduler
 238      * @param name thread name
 239      * @param characteristics characteristics
 240      * @param task the task to execute
 241      */
 242     VirtualThread(VirtualThreadScheduler scheduler,
 243                   String name,
 244                   int characteristics,
 245                   Runnable task) {
 246         super(name, characteristics, /*bound*/ false);
 247         Objects.requireNonNull(task);
 248 
 249         // use default scheduler if not provided
 250         if (scheduler == null) {
 251             scheduler = DEFAULT_SCHEDULER;





 252         }
 253 
 254         this.scheduler = scheduler;
 255         this.cont = new VThreadContinuation(this, task);
 256         this.runContinuation = this::runContinuation;
 257     }
 258 
 259     /**
 260      * The continuation that a virtual thread executes.
 261      */
 262     private static class VThreadContinuation extends Continuation {
 263         VThreadContinuation(VirtualThread vthread, Runnable task) {
 264             super(VTHREAD_SCOPE, wrap(vthread, task));
 265         }
 266         @Override
 267         protected void onPinned(Continuation.Pinned reason) {
 268         }
 269         private static Runnable wrap(VirtualThread vthread, Runnable task) {
 270             return new Runnable() {
 271                 @Hidden

 327                 afterYield();
 328             }
 329         }
 330     }
 331 
 332     /**
 333      * Cancel timeout task when continuing after timed-park or timed-wait.
 334      * The timeout task may be executing, or may have already completed.
 335      */
 336     private void cancelTimeoutTask() {
 337         if (timeoutTask != null) {
 338             timeoutTask.cancel(false);
 339             timeoutTask = null;
 340         }
 341     }
 342 
 343     /**
 344      * Submits the runContinuation task to the scheduler. For the default scheduler,
 345      * and calling it on a worker thread, the task will be pushed to the local queue,
 346      * otherwise it will be pushed to an external submission queue.

 347      * @param retryOnOOME true to retry indefinitely if OutOfMemoryError is thrown
 348      * @throws RejectedExecutionException
 349      */
 350     private void submitRunContinuation(boolean retryOnOOME) {
 351         boolean done = false;
 352         while (!done) {
 353             try {
 354                 // Pin the continuation to prevent the virtual thread from unmounting
 355                 // when submitting a task. For the default scheduler this ensures that
 356                 // the carrier doesn't change when pushing a task. For other schedulers
 357                 // it avoids deadlock that could arise due to carriers and virtual
 358                 // threads contending for a lock.
 359                 if (currentThread().isVirtual()) {
 360                     Continuation.pin();
 361                     try {
 362                         scheduler.execute(this, runContinuation);
 363                     } finally {
 364                         Continuation.unpin();
 365                     }
 366                 } else {
 367                     scheduler.execute(this, runContinuation);
 368                 }
 369                 done = true;
 370             } catch (RejectedExecutionException ree) {
 371                 submitFailed(ree);
 372                 throw ree;
 373             } catch (OutOfMemoryError e) {
 374                 if (retryOnOOME) {
 375                     U.park(false, 100_000_000); // 100ms
 376                 } else {
 377                     throw e;
 378                 }
 379             }
 380         }
 381     }
 382 


















 383     /**
 384      * Submits the runContinuation task to the scheduler. For the default scheduler,
 385      * and calling it on a worker thread, the task will be pushed to the local queue,
 386      * otherwise it will be pushed to an external submission queue.
 387      * If OutOfMemoryError is thrown then the submit will be retried until it succeeds.
 388      * @throws RejectedExecutionException
 389      */
 390     private void submitRunContinuation() {
 391         submitRunContinuation(true);
 392     }
 393 
 394     /**
 395      * Lazy submit the runContinuation task if invoked on a carrier thread and its local
 396      * queue is empty. If not empty, or invoked by another thread, then this method works
 397      * like submitRunContinuation and just submits the task to the scheduler.
 398      * If OutOfMemoryError is thrown then the submit will be retried until it succeeds.
 399      * @throws RejectedExecutionException
 400      * @see ForkJoinPool#lazySubmit(ForkJoinTask)
 401      */
 402     private void lazySubmitRunContinuation() {
 403         if (scheduler == DEFAULT_SCHEDULER
 404                 && currentCarrierThread() instanceof CarrierThread ct
 405                 && ct.getQueuedTaskCount() == 0) {
 406             try {
 407                 ct.getPool().lazySubmit(ForkJoinTask.adapt(runContinuation));
 408             } catch (RejectedExecutionException ree) {
 409                 submitFailed(ree);
 410                 throw ree;
 411             } catch (OutOfMemoryError e) {
 412                 submitRunContinuation();
 413             }
 414         } else {
 415             submitRunContinuation();
 416         }
 417     }
 418 
 419     /**
 420      * Submits the runContinuation task to the scheduler. For the default scheduler, and
 421      * calling it a virtual thread that uses the default scheduler, the task will be
 422      * pushed to an external submission queue.
 423      * @throws RejectedExecutionException
 424      */
 425     private void externalSubmitRunContinuation() {
 426         if (scheduler == DEFAULT_SCHEDULER && currentCarrierThread() instanceof CarrierThread ct) {
 427             try {
 428                 ct.getPool().externalSubmit(ForkJoinTask.adapt(runContinuation));
 429             } catch (RejectedExecutionException ree) {
 430                 submitFailed(ree);
 431                 throw ree;
 432             } catch (OutOfMemoryError e) {
 433                 submitRunContinuation();
 434             }
 435         } else {
 436             submitRunContinuation();
 437         }
 438     }
 439 
 440     /**
 441      * Submits the runContinuation task to the scheduler. For the default scheduler, and
 442      * calling it a virtual thread that uses the default scheduler, the task will be
 443      * pushed to an external submission queue. This method may throw OutOfMemoryError.
 444      * @throws RejectedExecutionException
 445      * @throws OutOfMemoryError
 446      */
 447     private void externalSubmitRunContinuationOrThrow() {
 448         if (scheduler == DEFAULT_SCHEDULER && currentCarrierThread() instanceof CarrierThread ct) {
 449             try {
 450                 ct.getPool().externalSubmit(ForkJoinTask.adapt(runContinuation));
 451             } catch (RejectedExecutionException ree) {
 452                 submitFailed(ree);
 453                 throw ree;
 454             }
 455         } else {
 456             submitRunContinuation(false);
 457         }
 458     }
 459 
 460     /**
 461      * If enabled, emits a JFR VirtualThreadSubmitFailedEvent.
 462      */
 463     private void submitFailed(RejectedExecutionException ree) {
 464         var event = new VirtualThreadSubmitFailedEvent();
 465         if (event.isEnabled()) {
 466             event.javaThreadId = threadId();
 467             event.exceptionMessage = ree.getMessage();
 468             event.commit();
 469         }
 470     }
 471 
 472     /**
 473      * Runs a task in the context of this virtual thread.
 474      */
 475     private void run(Runnable task) {
 476         assert Thread.currentThread() == this && state == RUNNING;

 591                 long timeout = this.timeout;
 592                 assert timeout > 0;
 593                 timeoutTask = schedule(this::parkTimeoutExpired, timeout, NANOSECONDS);
 594                 setState(newState = TIMED_PARKED);
 595             }
 596 
 597             // may have been unparked while parking
 598             if (parkPermit && compareAndSetState(newState, UNPARKED)) {
 599                 // lazy submit if local queue is empty
 600                 lazySubmitRunContinuation();
 601             }
 602             return;
 603         }
 604 
 605         // Thread.yield
 606         if (s == YIELDING) {
 607             setState(YIELDED);
 608 
 609             // external submit if there are no tasks in the local task queue
 610             if (currentThread() instanceof CarrierThread ct && ct.getQueuedTaskCount() == 0) {
 611                 externalSubmitRunContinuation();
 612             } else {
 613                 submitRunContinuation();
 614             }
 615             return;
 616         }
 617 
 618         // blocking on monitorenter
 619         if (s == BLOCKING) {
 620             setState(BLOCKED);
 621 
 622             // may have been unblocked while blocking
 623             if (blockPermit && compareAndSetState(BLOCKED, UNBLOCKED)) {
 624                 // lazy submit if local queue is empty
 625                 lazySubmitRunContinuation();
 626             }
 627             return;
 628         }
 629 
 630         // Object.wait
 631         if (s == WAITING || s == TIMED_WAITING) {
 632             int newState;
 633             boolean interruptable = interruptableWait;
 634             if (s == WAITING) {
 635                 setState(newState = WAIT);
 636             } else {
 637                 // For timed-wait, a timeout task is scheduled to execute. The timeout
 638                 // task will change the thread state to UNBLOCKED and submit the thread
 639                 // to the scheduler. A sequence number is used to ensure that the timeout
 640                 // task only unblocks the thread for this timed-wait. We synchronize with
 641                 // the timeout task to coordinate access to the sequence number and to
 642                 // ensure the timeout task doesn't execute until the thread has got to
 643                 // the TIMED_WAIT state.
 644                 long timeout = this.timeout;
 645                 assert timeout > 0;
 646                 synchronized (timedWaitLock()) {
 647                     byte seqNo = ++timedWaitSeqNo;
 648                     timeoutTask = schedule(() -> waitTimeoutExpired(seqNo), timeout, MILLISECONDS);
 649                     setState(newState = TIMED_WAIT);
 650                 }
 651             }
 652 
 653             // may have been notified while in transition to wait state
 654             if (notified && compareAndSetState(newState, BLOCKED)) {
 655                 // may have even been unblocked already
 656                 if (blockPermit && compareAndSetState(BLOCKED, UNBLOCKED)) {
 657                     submitRunContinuation();
 658                 }
 659                 return;
 660             }
 661 
 662             // may have been interrupted while in transition to wait state
 663             if (interruptable && interrupted && compareAndSetState(newState, UNBLOCKED)) {
 664                 submitRunContinuation();
 665                 return;
 666             }
 667             return;
 668         }
 669 
 670         assert false;
 671     }
 672 
 673     /**
 674      * Invoked after the continuation completes.
 675      */
 676     private void afterDone() {
 677         afterDone(true);
 678     }
 679 
 680     /**
 681      * Invoked after the continuation completes (or start failed). Sets the thread
 682      * state to TERMINATED and notifies anyone waiting for the thread to terminate.
 683      *

1430     @IntrinsicCandidate
1431     @JvmtiMountTransition
1432     private native void notifyJvmtiMount(boolean hide);
1433 
1434     @IntrinsicCandidate
1435     @JvmtiMountTransition
1436     private native void notifyJvmtiUnmount(boolean hide);
1437 
1438     @IntrinsicCandidate
1439     private static native void notifyJvmtiDisableSuspend(boolean enter);
1440 
1441     private static native void registerNatives();
1442     static {
1443         registerNatives();
1444 
1445         // ensure VTHREAD_GROUP is created, may be accessed by JVMTI
1446         var group = Thread.virtualThreadGroup();
1447     }
1448 
1449     /**
1450      * Loads a VirtualThreadScheduler with the given class name to use at the
1451      * default scheduler. The class is public in an exported package, has a public
1452      * one-arg or no-arg constructor, and is visible to the system class loader.
1453      */
1454     private static VirtualThreadScheduler createCustomDefaultScheduler(String cn) {
1455         try {
1456             Class<?> clazz = Class.forName(cn, true, ClassLoader.getSystemClassLoader());
1457             VirtualThreadScheduler scheduler;
1458             try {
1459                 // 1-arg constructor
1460                 Constructor<?> ctor = clazz.getConstructor(VirtualThreadScheduler.class);
1461                 var builtin = createDefaultForkJoinPoolScheduler();
1462                 scheduler = (VirtualThreadScheduler) ctor.newInstance(builtin.externalView());
1463             } catch (NoSuchMethodException e) {
1464                 // 0-arg constructor
1465                 Constructor<?> ctor = clazz.getConstructor();
1466                 scheduler = (VirtualThreadScheduler) ctor.newInstance();
1467             }
1468             System.err.println("""
1469                 WARNING: Using custom default scheduler, this is an experimental feature!""");
1470             return scheduler;
1471         } catch (Exception ex) {
1472             throw new Error(ex);
1473         }
1474     }
1475 
1476     /**
1477      * Creates the built-in default ForkJoinPool scheduler.
1478      */
1479     private static BuiltinDefaultScheduler createDefaultForkJoinPoolScheduler() {

1480         int parallelism, maxPoolSize, minRunnable;
1481         String parallelismValue = System.getProperty("jdk.virtualThreadScheduler.parallelism");
1482         String maxPoolSizeValue = System.getProperty("jdk.virtualThreadScheduler.maxPoolSize");
1483         String minRunnableValue = System.getProperty("jdk.virtualThreadScheduler.minRunnable");
1484         if (parallelismValue != null) {
1485             parallelism = Integer.parseInt(parallelismValue);
1486         } else {
1487             parallelism = Runtime.getRuntime().availableProcessors();
1488         }
1489         if (maxPoolSizeValue != null) {
1490             maxPoolSize = Integer.parseInt(maxPoolSizeValue);
1491             parallelism = Integer.min(parallelism, maxPoolSize);
1492         } else {
1493             maxPoolSize = Integer.max(parallelism, 256);
1494         }
1495         if (minRunnableValue != null) {
1496             minRunnable = Integer.parseInt(minRunnableValue);
1497         } else {
1498             minRunnable = Integer.max(parallelism / 2, 1);
1499         }
1500         return new BuiltinDefaultScheduler(parallelism, maxPoolSize, minRunnable);
1501     }
1502 
1503     /**
1504      * The built-in default ForkJoinPool scheduler.
1505      */
1506     private static class BuiltinDefaultScheduler
1507             extends ForkJoinPool implements VirtualThreadScheduler {
1508 
1509         private static final StableValue<VirtualThreadScheduler> VIEW = StableValue.of();
1510 
1511         BuiltinDefaultScheduler(int parallelism, int maxPoolSize, int minRunnable) {
1512             ForkJoinWorkerThreadFactory factory = pool -> new CarrierThread(pool);
1513             Thread.UncaughtExceptionHandler handler = (t, e) -> { };
1514             boolean asyncMode = true; // FIFO
1515             super(parallelism, factory, handler, asyncMode,
1516                     0, maxPoolSize, minRunnable, pool -> true, 30, SECONDS);
1517         }
1518 
1519         @Override
1520         public void execute(Thread vthread, Runnable task) {
1521             execute(ForkJoinTask.adapt(task));
1522         }
1523 
1524         /**
1525          * Wraps the scheduler to avoid leaking a direct reference.
1526          */
1527         VirtualThreadScheduler externalView() {
1528             VirtualThreadScheduler builtin = this;
1529             return VIEW.orElseSet(() -> {
1530                 return new VirtualThreadScheduler() {
1531                     @Override
1532                     public void execute(Thread thread, Runnable task) {
1533                         Objects.requireNonNull(thread);
1534                         if (thread instanceof VirtualThread vthread) {
1535                             VirtualThreadScheduler scheduler = vthread.scheduler;
1536                             if (scheduler == this || scheduler == DEFAULT_SCHEDULER) {
1537                                 builtin.execute(thread, task);
1538                             } else {
1539                                 throw new IllegalArgumentException();
1540                             }
1541                         } else {
1542                             throw new UnsupportedOperationException();
1543                         }
1544                     }
1545                 };
1546             });
1547         }
1548     }
1549 
1550     /**
1551      * Schedule a runnable task to run after a delay.
1552      */
1553     private Future<?> schedule(Runnable command, long delay, TimeUnit unit) {
1554         if (scheduler instanceof ForkJoinPool pool) {
1555             return pool.schedule(command, delay, unit);
1556         } else {
1557             return DelayedTaskSchedulers.schedule(command, delay, unit);
1558         }
1559     }
1560 
1561     /**
1562      * Supports scheduling a runnable task to run after a delay. It uses a number
1563      * of ScheduledThreadPoolExecutor instances to reduce contention on the delayed
1564      * work queue used. This class is used when using a custom scheduler.
1565      */
1566     private static class DelayedTaskSchedulers {
1567         private static final ScheduledExecutorService[] INSTANCE = createDelayedTaskSchedulers();

1617                 assert changed;
1618                 vthread.unblock();
1619 
1620                 vthread = nextThread;
1621             }
1622         }
1623     }
1624 
1625     /**
1626      * Retrieves the list of virtual threads that are waiting to be unblocked, waiting
1627      * if necessary until a list of one or more threads becomes available.
1628      */
1629     private static native VirtualThread takeVirtualThreadListToUnblock();
1630 
1631     static {
1632         var unblocker = InnocuousThread.newThread("VirtualThread-unblocker",
1633                 VirtualThread::unblockVirtualThreads);
1634         unblocker.setDaemon(true);
1635         unblocker.start();
1636     }
1637 }
< prev index next >