1 /* 2 * Copyright (c) 2018, 2022, 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. 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 26 package jdk.internal.misc; 27 28 import java.util.concurrent.TimeUnit; 29 import java.util.concurrent.RejectedExecutionException; 30 import jdk.internal.access.JavaLangAccess; 31 import jdk.internal.access.SharedSecrets; 32 33 /** 34 * Defines static methods to support execution in the context of a virtual thread. 35 */ 36 public final class VirtualThreads { 37 private static final JavaLangAccess JLA; 38 static { 39 JLA = SharedSecrets.getJavaLangAccess(); 40 if (JLA == null) { 41 throw new InternalError("JavaLangAccess not setup"); 42 } 43 } 44 private VirtualThreads() { } 45 46 /** 47 * Parks the current virtual thread until it is unparked or interrupted. 48 * If already unparked then the parking permit is consumed and this method 49 * completes immediately (meaning it doesn't yield). It also completes 50 * immediately if the interrupt status is set. 51 * @throws WrongThreadException if the current thread is not a virtual thread 52 */ 53 public static void park() { 54 JLA.parkVirtualThread(); 55 } 56 57 /** 58 * Parks the current virtual thread up to the given waiting time or until it 59 * is unparked or interrupted. If already unparked then the parking permit is 60 * consumed and this method completes immediately (meaning it doesn't yield). 61 * It also completes immediately if the interrupt status is set or the waiting 62 * time is {@code <= 0}. 63 * @param nanos the maximum number of nanoseconds to wait 64 * @throws WrongThreadException if the current thread is not a virtual thread 65 */ 66 public static void park(long nanos) { 67 JLA.parkVirtualThread(nanos); 68 } 69 70 /** 71 * Parks the current virtual thread until the given deadline or until is is 72 * unparked or interrupted. If already unparked then the parking permit is 73 * consumed and this method completes immediately (meaning it doesn't yield). 74 * It also completes immediately if the interrupt status is set or the 75 * deadline has past. 76 * @param deadline absolute time, in milliseconds, from the epoch 77 * @throws WrongThreadException if the current thread is not a virtual thread 78 */ 79 public static void parkUntil(long deadline) { 80 long millis = deadline - System.currentTimeMillis(); 81 long nanos = TimeUnit.NANOSECONDS.convert(millis, TimeUnit.MILLISECONDS); 82 park(nanos); 83 } 84 85 /** 86 * Re-enables a virtual thread for scheduling. If the thread was parked then 87 * it will be unblocked, otherwise its next attempt to park will not block 88 * @param thread the virtual thread to unpark 89 * @throws IllegalArgumentException if the thread is not a virtual thread 90 * @throws RejectedExecutionException if the scheduler cannot accept a task 91 */ 92 public static void unpark(Thread thread) { 93 JLA.unparkVirtualThread(thread); 94 } 95 }