From 4516bf36d71cb14fa851d3aea5c59cbc2c96a52c Mon Sep 17 00:00:00 2001 From: Alan Bateman Date: Sat, 16 Nov 2019 14:14:20 +0000 Subject: [PATCH] Lightweight -> virtual thread --- .../share/classes/java/lang/Object.java | 2 +- .../share/classes/java/lang/StringCoding.java | 6 +- .../share/classes/java/lang/System.java | 6 +- .../share/classes/java/lang/Thread.java | 332 +++++++++--------- .../java/util/concurrent/ForkJoinPool.java | 4 +- .../util/concurrent/locks/LockSupport.java | 30 +- .../jdk/internal/access/JavaLangAccess.java | 12 +- .../jdk/internal/math/FloatingDecimal.java | 2 +- .../math/FormattedFloatingDecimal.java | 2 +- .../classes/jdk/internal/misc/Blocker.java | 4 +- .../internal/misc/TerminatingThreadLocal.java | 4 +- ...weightThreads.java => VirtualThreads.java} | 18 +- .../NativeConstructorAccessorImpl.java | 2 +- .../reflect/NativeMethodAccessorImpl.java | 2 +- .../sun/nio/ch/DatagramChannelImpl.java | 6 +- .../classes/sun/nio/ch/NioSocketImpl.java | 18 +- .../share/classes/sun/nio/ch/SelChImpl.java | 8 +- .../sun/nio/ch/ServerSocketChannelImpl.java | 4 +- .../classes/sun/nio/ch/SocketChannelImpl.java | 10 +- .../classes/sun/nio/cs/ThreadLocalCoders.java | 2 +- .../unix/classes/sun/nio/ch/NativeThread.java | 18 +- .../classes/sun/nio/ch/SinkChannelImpl.java | 2 +- .../classes/sun/nio/ch/SourceChannelImpl.java | 2 +- .../classes/sun/nio/ch/NativeThread.java | 8 +- .../DoContinueSingleStepTest.java | 6 +- .../jvmti/FiberTest/MyPackage/FiberTest.java | 4 +- test/jdk/java/lang/Thread/BuilderTest.java | 114 +++--- .../{lightweight => virtual}/Collectable.java | 14 +- .../{lightweight => virtual}/Locking.java | 40 +-- .../{lightweight => virtual}/NetSockets.java | 38 +- .../{lightweight => virtual}/NioChannels.java | 106 +++--- .../{lightweight => virtual}/Parking.java | 74 ++-- .../{lightweight => virtual}/TestHelper.java | 10 +- .../{lightweight => virtual}/ThreadAPI.java | 250 ++++++------- .../ThreadLocals.java | 26 +- .../{lightweight => virtual}/WaitNotify.java | 20 +- 36 files changed, 602 insertions(+), 604 deletions(-) rename src/java.base/share/classes/jdk/internal/misc/{LightweightThreads.java => VirtualThreads.java} (81%) rename test/jdk/java/lang/Thread/{lightweight => virtual}/Collectable.java (81%) rename test/jdk/java/lang/Thread/{lightweight => virtual}/Locking.java (81%) rename test/jdk/java/lang/Thread/{lightweight => virtual}/NetSockets.java (91%) rename test/jdk/java/lang/Thread/{lightweight => virtual}/NioChannels.java (88%) rename test/jdk/java/lang/Thread/{lightweight => virtual}/Parking.java (72%) rename test/jdk/java/lang/Thread/{lightweight => virtual}/TestHelper.java (89%) rename test/jdk/java/lang/Thread/{lightweight => virtual}/ThreadAPI.java (83%) rename test/jdk/java/lang/Thread/{lightweight => virtual}/ThreadLocals.java (85%) rename test/jdk/java/lang/Thread/{lightweight => virtual}/WaitNotify.java (85%) diff --git a/src/java.base/share/classes/java/lang/Object.java b/src/java.base/share/classes/java/lang/Object.java index 9eac0a2b6b0..c801ab37ed3 100644 --- a/src/java.base/share/classes/java/lang/Object.java +++ b/src/java.base/share/classes/java/lang/Object.java @@ -347,7 +347,7 @@ public final void wait(long timeoutMillis) throws InterruptedException { wait0(timeoutMillis); } catch (InterruptedException e) { Thread thread = Thread.currentThread(); - if (thread.isLightweight()) { + if (thread.isVirtual()) { thread.clearInterrupt(); } throw e; diff --git a/src/java.base/share/classes/java/lang/StringCoding.java b/src/java.base/share/classes/java/lang/StringCoding.java index 4045cc1ca6c..27d54d70880 100644 --- a/src/java.base/share/classes/java/lang/StringCoding.java +++ b/src/java.base/share/classes/java/lang/StringCoding.java @@ -73,7 +73,7 @@ private StringCoding() { } private static final Charset UTF_8 = sun.nio.cs.UTF_8.INSTANCE; private static T deref(ThreadLocal> tl) { - if (!Thread.currentThread().isLightweight()) { + if (!Thread.currentThread().isVirtual()) { SoftReference sr = tl.get(); if (sr != null) return sr.get(); @@ -82,7 +82,7 @@ private static T deref(ThreadLocal> tl) { } private static void set(ThreadLocal> tl, T ob) { - if (!Thread.currentThread().isLightweight()) + if (!Thread.currentThread().isVirtual()) tl.set(new SoftReference<>(ob)); } @@ -533,7 +533,7 @@ protected StringCoding.Result initialValue() { }}; private static StringCoding.Result cachedResultObject() { - if (Thread.currentThread().isLightweight()) { + if (Thread.currentThread().isVirtual()) { return new StringCoding.Result(); } else { return resultCached.get(); diff --git a/src/java.base/share/classes/java/lang/System.java b/src/java.base/share/classes/java/lang/System.java index 112b6284f0e..84d645e6aa5 100644 --- a/src/java.base/share/classes/java/lang/System.java +++ b/src/java.base/share/classes/java/lang/System.java @@ -2306,15 +2306,15 @@ public void setCarrierThreadLocal(ThreadLocal local, T value) { local.setCarrierThreadLocal(value); } - public void parkLightweightThread() { + public void parkVirtualThread() { Fiber.park(); } - public void parkLightweightThread(long nanos) { + public void parkVirtualThread(long nanos) { Fiber.parkNanos(nanos); } - public void unparkLightweightThread(Thread thread) { + public void unparkVirtualThread(Thread thread) { ((Fiber) thread).unpark(); } }); diff --git a/src/java.base/share/classes/java/lang/Thread.java b/src/java.base/share/classes/java/lang/Thread.java index a44bb1d6951..c11be42154f 100644 --- a/src/java.base/share/classes/java/lang/Thread.java +++ b/src/java.base/share/classes/java/lang/Thread.java @@ -64,13 +64,12 @@ * Kernel threads are suitable for executing all tasks but they are limited * resource. * - *

{@code Thread} also supports the creation of virtual threads, - * sometimes known as lightweight threads, that are scheduled by the Java - * virtual machine rather than the operating system. Virtual threads will - * typically require few resources, a single Java virtual machine may support - * millions of virtual threads. Virtual threads are suitable for executing tasks - * that spend most of the time blocked, often waiting for synchronous blocking - * I/O operations to complete. + *

{@code Thread} also supports the creation of virtual threads that + * are scheduled by the Java virtual machine rather than the operating system. + * Virtual threads will typically require few resources and a single Java virtual + * machine may support millions of virtual threads. Virtual threads are suitable + * for executing tasks that spend most of the time blocked, often waiting for + * synchronous blocking I/O operations to complete. * Virtual threads execute on a pool of carrier threads, essentially * a pool of kernel threads that have been created and allocated to support the * execution of virtual threads. Locking and I/O operations are the scheduling @@ -117,7 +116,7 @@ class Thread implements Runnable { // holds fields for heavyweight threads private static class FieldHolder { final ThreadGroup group; - final Runnable target; + final Runnable task; final long stackSize; int priority; boolean daemon; @@ -125,12 +124,12 @@ private static class FieldHolder { boolean stillborn; FieldHolder(ThreadGroup group, - Runnable target, + Runnable task, long stackSize, int priority, boolean daemon) { this.group = group; - this.target = target; + this.task = task; this.stackSize = stackSize; this.priority = priority; this.daemon = daemon; @@ -442,7 +441,7 @@ private static ClassLoader contextClassLoader(Thread parent) { * @param g the Thread group * @param name the name of the new Thread * @param characteristics thread characteristics - * @param target the object whose run() method gets called + * @param task the object whose run() method gets called * @param stackSize the desired stack size for the new thread, or * zero to indicate that this parameter is to be ignored. @@ -450,7 +449,7 @@ private static ClassLoader contextClassLoader(Thread parent) { * AccessController.getContext() if null * @throws IllegalArgumentException if invalid characteristics are specified */ - private Thread(ThreadGroup g, String name, int characteristics, Runnable target, + private Thread(ThreadGroup g, String name, int characteristics, Runnable task, long stackSize, AccessControlContext acc) { if (name == null) { throw new NullPointerException("name cannot be null"); @@ -518,7 +517,7 @@ private Thread(ThreadGroup g, String name, int characteristics, Runnable target, priority = parent.getPriority(); daemon = parent.isDaemon(); } - this.holder = new FieldHolder(g, target, stackSize, priority, daemon); + this.holder = new FieldHolder(g, task, stackSize, priority, daemon); } /** @@ -538,7 +537,7 @@ private Thread(ThreadGroup g, String name, int characteristics, Runnable target, this.name = name; this.tid = nextThreadID(); this.contextClassLoader = contextClassLoader(parent); - this.inheritedAccessControlContext = LightweightThreads.ACCESS_CONTROL_CONTEXT; + this.inheritedAccessControlContext = VirtualThreads.ACCESS_CONTROL_CONTEXT; // thread locals if ((characteristics & NO_THREAD_LOCALS) != 0) { @@ -561,33 +560,33 @@ private Thread(ThreadGroup g, String name, int characteristics, Runnable target, * @apiNote The following are examples using the builder: * *

{@code
-     *   // Create an unnamed thread that is scheduled by the Java virtual machine
-     *   Thread thread1 = Thread.builder().lightweight().target(...).build();
-     *
-     *   // Create an named thread that is scheduled by the Java virtual machine
-     *   Thread thread2 = Thread.builder().lightweight().name("duke").target(...).build();
-     *
-     *   // Create and start a thread that is scheduled by the Java virtual machine
-     *   Thread thread = Thread.builder().lightweight().target(...).start();
-     *
-     *   // A ThreadFactory that creates threads scheduled by the Java virtual machine
-     *   ThreadFactory factory = Thread.builder().lightweight().factory();
-     *
-     *   // A ThreadFactory that creates threads scheduled by the given scheduler
-     *   ExecutorService executor = ...
-     *   ThreadFactory factory = Thread.builder().lightweight().executor(executor).factory();
-     *
      *   // Create a daemon thread that is scheduled by the operating system
      *   Thread thread = Thread.builder()
      *                 .name("duke")
      *                 .daemon(true)
      *                 .priority(Thread.NORM_PRIORITY)
      *                 .inheritThreadLocals()
-     *                 .target(...)
+     *                 .task(...)
      *                 .build();
      *
-     *    // A ThreadFactory that creates daemon threads named "worker-%d"
-     *    ThreadFactory factory = Thread.builder().daemon(true).name("worker-", 0).factory();
+     *   // A ThreadFactory that creates daemon threads named "worker-0", "worker-1", ...
+     *   ThreadFactory factory = Thread.builder().daemon(true).name("worker-", 0).factory();
+     *
+     *   // Create an unnamed virtual thread
+     *   Thread thread1 = Thread.builder().virtual().task(...).build();
+     *
+     *   // Create a named virtual thread
+     *   Thread thread2 = Thread.builder().virtual().name("duke").task(...).build();
+     *
+     *   // Create and start a virtual thread
+     *   Thread thread = Thread.builder().virtual().task(...).start();
+     *
+     *   // A ThreadFactory that creates virtual threads
+     *   ThreadFactory factory = Thread.builder().virtual().factory();
+     *
+     *   // A ThreadFactory that creates virtual threads and uses a custom scheduler
+     *   Executor scheduler = ...
+     *   ThreadFactory factory = Thread.builder().virtual().scheduler(scheduler).factory();
      * }
* * @return A builder for creating {@code Thread} or {@code ThreadFactory} objects. @@ -612,8 +611,7 @@ public static Builder builder() { * * *

A {@code Builder} is not thread safe. The {@code ThreadFactory} - * returned by the builder's {@code factory() method} is immutable and safe - * for use by multiple concurrent threads. + * returned by the builder's {@code factory() method} is thread safe. * *

Unless otherwise specified, passing a null argument to a method in * this interface causes a {@code NullPointerException} to be thrown. @@ -637,19 +635,18 @@ public interface Builder { * * @param group the thread group * @return this builder - * @throws IllegalStateException if this is a builder for a thread - * that is scheduled by the Java virtual machine + * @throws IllegalStateException if this is a builder for a virtual thread */ Builder group(ThreadGroup group); /** - * Sets the executor. - * @param executor the executor + * Sets the scheduler. + * @param scheduler the scheduler * @return this builder * @throws IllegalStateException if this is a builder for a thread * that will be scheduled by the operating system */ - Builder executor(Executor executor); + Builder scheduler(Executor scheduler); /** * Sets the thread name. @@ -674,7 +671,7 @@ public interface Builder { * @return this builder * @throws IllegalStateException if a thread group has been set */ - Builder lightweight(); + Builder virtual(); /** * Disallow threads locals @@ -710,31 +707,31 @@ public interface Builder { Builder priority(int priority); /** - * The thread is managed. + * The thread is managed. * @return this builder */ Builder managed(); /** - * Sets the target object for the thread to run. - * @param target the target object + * Sets the task for the thread to run. + * @param task the task to run * @return this builder */ - Builder target(Runnable target); + Builder task(Runnable task); /** * Creates a new unstarted {@code Thread} from the current state of the * builder. * * @return a new unstarted Thread - * @throws IllegalStateException if the target object to run object has not been set + * @throws IllegalStateException if the task object to run object has not been set */ Thread build(); /** * Returns a {@code ThreadFactory} to create threads from the current - * state of the builder. The returned thread factory is immutable and safe - * for use by multiple concurrent threads. + * state of the builder. The returned thread factory is safe for use by + * multiple concurrent threads. * * @return a thread factory to create threads */ @@ -760,23 +757,23 @@ default Thread start() { private static class BuilderImpl implements Builder { private ThreadGroup group; - private Executor executor; + private Executor scheduler; private String name; private int counter; - private boolean lightweight; + private boolean virtual; private boolean disallowThreadLocals; private boolean inheritThreadLocals; private boolean daemon; private boolean daemonChanged; private int priority; - private Runnable target; + private Runnable task; BuilderImpl() { } private int characteristics() { int characteristics = 0; - if (lightweight) - characteristics |= Thread.LIGHTWEIGHT; + if (virtual) + characteristics |= Thread.VIRTUAL; if (disallowThreadLocals) characteristics |= Thread.NO_THREAD_LOCALS; if (inheritThreadLocals) @@ -787,18 +784,18 @@ private int characteristics() { @Override public Builder group(ThreadGroup group) { Objects.requireNonNull(group); - if (lightweight) + if (virtual) throw new IllegalStateException(); this.group = group; return this; } @Override - public Builder executor(Executor executor) { - Objects.requireNonNull(executor); - if (!lightweight) + public Builder scheduler(Executor scheduler) { + Objects.requireNonNull(scheduler); + if (!virtual) throw new IllegalStateException(); - this.executor = executor; + this.scheduler = scheduler; return this; } @@ -820,10 +817,10 @@ public Builder name(String name, int start) { } @Override - public Builder lightweight() { + public Builder virtual() { if (group != null) throw new IllegalStateException(); - lightweight = true; + virtual = true; return this; } @@ -866,19 +863,19 @@ public Builder managed() { @Override public Thread build() { - Runnable target = this.target; - if (target == null) - throw new IllegalStateException("No target specified"); + Runnable task = this.task; + if (task == null) + throw new IllegalStateException("No task specified"); int characteristics = characteristics(); - if ((characteristics & Thread.LIGHTWEIGHT) != 0) { + if ((characteristics & Thread.VIRTUAL) != 0) { String name = this.name; if (name == null) { name = ""; } else if (counter >= 0) { name = name + (counter++); } - return new Fiber(executor, name, characteristics, target); + return new Fiber(scheduler, name, characteristics, task); } else { String name = this.name; if (name == null) { @@ -886,7 +883,7 @@ public Thread build() { } else if (counter >= 0) { name = name + (counter++); } - Thread thread = new Thread(group, name, characteristics, target, 0, null); + Thread thread = new Thread(group, name, characteristics, task, 0, null); if (daemonChanged) thread.setDaemon(daemon); if (priority != 0) @@ -896,16 +893,16 @@ public Thread build() { } @Override - public Builder target(Runnable target) { - this.target = Objects.requireNonNull(target); + public Builder task(Runnable task) { + this.task = Objects.requireNonNull(task); return this; } @Override public ThreadFactory factory() { int characteristics = characteristics(); - if ((characteristics & Thread.LIGHTWEIGHT) != 0) { - return new LightweightThreadFactory(executor, name, counter, characteristics); + if ((characteristics & Thread.VIRTUAL) != 0) { + return new VirtualThreadFactory(scheduler, name, counter, characteristics); } else { return new DinosaurThreadFactory(group, name, counter, characteristics, daemon, priority); } @@ -943,31 +940,28 @@ int next() { } } - private static class LightweightThreadFactory extends CountingThreadFactory { - private final Executor executor; + private static class VirtualThreadFactory extends CountingThreadFactory { + private final Executor scheduler; private String name; private final int characteristics; - LightweightThreadFactory(Executor executor, - String name, - int start, - int characteristics) { + VirtualThreadFactory(Executor scheduler, String name, int start, int characteristics) { super(start); - this.executor = executor; + this.scheduler = scheduler; this.name = name; this.characteristics = characteristics; } @Override - public Thread newThread(Runnable target) { - Objects.requireNonNull(target); + public Thread newThread(Runnable task) { + Objects.requireNonNull(task); String name = this.name; if (name == null) { name = ""; } else if (hasCounter()) { name += next(); } - return new Fiber(executor, name, characteristics, target); + return new Fiber(scheduler, name, characteristics, task); } } @@ -993,15 +987,15 @@ private static class DinosaurThreadFactory extends CountingThreadFactory { } @Override - public Thread newThread(Runnable target) { - Objects.requireNonNull(target); + public Thread newThread(Runnable task) { + Objects.requireNonNull(task); String name = this.name; if (name == null) { name = "Thread-" + nextThreadNum(); } else if (hasCounter()) { name += next(); } - Thread thread = new Thread(group, name, characteristics, target, 0, null); + Thread thread = new Thread(group, name, characteristics, task, 0, null); if (daemon) thread.setDaemon(true); if (priority != 0) @@ -1036,17 +1030,17 @@ public Thread() { /** * Allocates a new {@code Thread} object. This constructor has the same * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} - * {@code (null, target, gname)}, where {@code gname} is a newly generated + * {@code (null, task, gname)}, where {@code gname} is a newly generated * name. Automatically generated names are of the form * {@code "Thread-"+}n, where n is an integer. * - * @param target + * @param task * the object whose {@code run} method is invoked when this thread * is started. If {@code null}, this classes {@code run} method does * nothing. */ - public Thread(Runnable target) { - this(null, target, "Thread-" + nextThreadNum(), 0); + public Thread(Runnable task) { + this(null, task, "Thread-" + nextThreadNum(), 0); } /** @@ -1054,14 +1048,14 @@ public Thread(Runnable target) { * but thread-local variables are not inherited. * This is not a public constructor. */ - Thread(Runnable target, AccessControlContext acc) { - this(null, "Thread-" + nextThreadNum(), 0, target, 0, acc); + Thread(Runnable task, AccessControlContext acc) { + this(null, "Thread-" + nextThreadNum(), 0, task, 0, acc); } /** * Allocates a new {@code Thread} object. This constructor has the same * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} - * {@code (group, target, gname)} ,where {@code gname} is a newly generated + * {@code (group, task, gname)} ,where {@code gname} is a newly generated * name. Automatically generated names are of the form * {@code "Thread-"+}n, where n is an integer. * @@ -1073,7 +1067,7 @@ public Thread(Runnable target) { * SecurityManager.getThreadGroup()} returns {@code null}, the group * is set to the current thread's thread group. * - * @param target + * @param task * the object whose {@code run} method is invoked when this thread * is started. If {@code null}, this thread's run method is invoked. * @@ -1081,8 +1075,8 @@ public Thread(Runnable target) { * if the current thread cannot create a thread in the specified * thread group */ - public Thread(ThreadGroup group, Runnable target) { - this(group, target, "Thread-" + nextThreadNum(), 0); + public Thread(ThreadGroup group, Runnable task) { + this(group, task, "Thread-" + nextThreadNum(), 0); } /** @@ -1124,21 +1118,21 @@ public Thread(ThreadGroup group, String name) { /** * Allocates a new {@code Thread} object. This constructor has the same * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} - * {@code (null, target, name)}. + * {@code (null, task, name)}. * - * @param target + * @param task * the object whose {@code run} method is invoked when this thread * is started. If {@code null}, this thread's run method is invoked. * * @param name * the name of the new thread */ - public Thread(Runnable target, String name) { - this(null, target, name, 0); + public Thread(Runnable task, String name) { + this(null, task, name, 0); } /** - * Allocates a new {@code Thread} object so that it has {@code target} + * Allocates a new {@code Thread} object so that it has {@code task} * as its run object, has the specified {@code name} as its name, * and belongs to the thread group referred to by {@code group}. * @@ -1170,7 +1164,7 @@ public Thread(Runnable target, String name) { * SecurityManager.getThreadGroup()} returns {@code null}, the group * is set to the current thread's thread group. * - * @param target + * @param task * the object whose {@code run} method is invoked when this thread * is started. If {@code null}, this thread's run method is invoked. * @@ -1181,12 +1175,12 @@ public Thread(Runnable target, String name) { * if the current thread cannot create a thread in the specified * thread group or cannot override the context class loader methods. */ - public Thread(ThreadGroup group, Runnable target, String name) { - this(group, target, name, 0); + public Thread(ThreadGroup group, Runnable task, String name) { + this(group, task, name, 0); } /** - * Allocates a new {@code Thread} object so that it has {@code target} + * Allocates a new {@code Thread} object so that it has {@code task} * as its run object, has the specified {@code name} as its name, * and belongs to the thread group referred to by {@code group}, and has * the specified stack size. @@ -1242,7 +1236,7 @@ public Thread(ThreadGroup group, Runnable target, String name) { * SecurityManager.getThreadGroup()} returns {@code null}, the group * is set to the current thread's thread group. * - * @param target + * @param task * the object whose {@code run} method is invoked when this thread * is started. If {@code null}, this thread's run method is invoked. * @@ -1259,12 +1253,12 @@ public Thread(ThreadGroup group, Runnable target, String name) { * * @since 1.4 */ - public Thread(ThreadGroup group, Runnable target, String name, long stackSize) { - this(group, name, Thread.INHERIT_THREAD_LOCALS, target, stackSize, null); + public Thread(ThreadGroup group, Runnable task, String name, long stackSize) { + this(group, name, Thread.INHERIT_THREAD_LOCALS, task, stackSize, null); } /** - * Allocates a new {@code Thread} object so that it has {@code target} + * Allocates a new {@code Thread} object so that it has {@code task} * as its run object, has the specified {@code name} as its name, * belongs to the thread group referred to by {@code group}, has * the specified {@code stackSize}, and inherits initial values for @@ -1292,7 +1286,7 @@ public Thread(ThreadGroup group, Runnable target, String name, long stackSize) { * SecurityManager.getThreadGroup()} returns {@code null}, the group * is set to the current thread's thread group. * - * @param target + * @param task * the object whose {@code run} method is invoked when this thread * is started. If {@code null}, this thread's run method is invoked. * @@ -1314,10 +1308,10 @@ public Thread(ThreadGroup group, Runnable target, String name, long stackSize) { * * @since 9 */ - public Thread(ThreadGroup group, Runnable target, String name, + public Thread(ThreadGroup group, Runnable task, String name, long stackSize, boolean inheritThreadLocals) { this(group, name, (inheritThreadLocals ? Thread.INHERIT_THREAD_LOCALS : 0), - target, stackSize, null); + task, stackSize, null); } /** @@ -1326,7 +1320,7 @@ public Thread(ThreadGroup group, Runnable target, String name, * * @since 99 */ - public static final int LIGHTWEIGHT = 1 << 0; + public static final int VIRTUAL = 1 << 0; /** * Characteristic value signifying that {@link ThreadLocal thread-locals} @@ -1357,7 +1351,7 @@ public Thread(ThreadGroup group, Runnable target, String name, public static final int MANAGED = 1 << 3; private static int validCharacteristics() { - return (LIGHTWEIGHT | NO_THREAD_LOCALS | INHERIT_THREAD_LOCALS | MANAGED); + return (VIRTUAL | NO_THREAD_LOCALS | INHERIT_THREAD_LOCALS | MANAGED); } private static void checkCharacteristics(int characteristics) { @@ -1380,25 +1374,27 @@ private static void checkCharacteristics(int characteristics) { * {@link #getPriority() priority}, and the {@link #getContextClassLoader() * context-class-loader} are inherited from the current thread. * - *

The characteristic {@linkplain Thread#LIGHTWEIGHT LIGHTWEIGHT} is + *

The characteristic {@linkplain Thread#VIRTUAL VIRTUAL} is * used to create a thread that is scheduled by the Java virtual machine * using the default scheduler. The default in this case is to only inherit * the {@link #getContextClassLoader() context-class-loader} from the current * thread. * + * @apiNote The characteristics will probably be replaced by an enum + * * @param characteristics characteristics of the thread - * @param target the object to run when the thread executes + * @param task the object to run when the thread executes * @throws IllegalArgumentException if an unknown characteristic or an invalid * combination of characteristic is specified - * @return an un-started lightweight thread + * @return an un-started virtual thread * * @since 99 */ - public static Thread newThread(int characteristics, Runnable target) { - if ((characteristics & LIGHTWEIGHT) != 0) { - return new Fiber(null, "", characteristics, target); + public static Thread newThread(int characteristics, Runnable task) { + if ((characteristics & VIRTUAL) != 0) { + return new Fiber(null, "", characteristics, task); } else { - return new Thread(null, "Thread-" + nextThreadNum(), characteristics, target, 0, null); + return new Thread(null, "Thread-" + nextThreadNum(), characteristics, task, 0, null); } } @@ -1412,26 +1408,28 @@ public static Thread newThread(int characteristics, Runnable target) { * {@link #getPriority() priority}, and the {@link #getContextClassLoader() * context-class-loader} are inherited from the current thread. * - *

The characteristic {@linkplain Thread#LIGHTWEIGHT LIGHTWEIGHT} is + *

The characteristic {@linkplain Thread#VIRTUAL VIRTUAL} is * used to create a thread that is scheduled by the Java virtual machine * using the default scheduler. The default in this case is to only inherit * the {@link #getContextClassLoader() context-class-loader} from the current * thread. * + * @apiNote The characteristics will probably be replaced by an enum + * * @param name the thread name * @param characteristics characteristics of the thread - * @param target the object to run when the thread executes + * @param task the object to run when the thread executes * @throws IllegalArgumentException if an unknown characteristic or an invalid * combination of characteristic is specified - * @return an un-started lightweight thread + * @return an un-started virtual thread * * @since 99 */ - public static Thread newThread(String name, int characteristics, Runnable target) { - if ((characteristics & LIGHTWEIGHT) != 0) { - return new Fiber(null, name, characteristics, target); + public static Thread newThread(String name, int characteristics, Runnable task) { + if ((characteristics & VIRTUAL) != 0) { + return new Fiber(null, name, characteristics, task); } else { - return new Thread(null, name, characteristics, target, 0, null); + return new Thread(null, name, characteristics, task, 0, null); } } @@ -1450,7 +1448,7 @@ public static Thread newThread(String name, int characteristics, Runnable target * * @since 99 */ - public final boolean isLightweight() { + public final boolean isVirtual() { return (this instanceof Fiber); } @@ -1506,8 +1504,8 @@ public synchronized void start() { * {@code Runnable} run object, then that * {@code Runnable} object's {@code run} method is called; * otherwise, this method does nothing and returns. - * This method does nothing when invoked on a {@link #isLightweight() - * lightweight} thread. + * This method does nothing when invoked on a {@linkplain #isVirtual() + * virtual} thread. *

* Subclasses of {@code Thread} should override this method. * @@ -1516,10 +1514,10 @@ public synchronized void start() { */ @Override public void run() { - if (!isLightweight()) { - Runnable target = holder.target; - if (target != null) { - target.run(); + if (!isVirtual()) { + Runnable task = holder.task; + if (task != null) { + task.run(); } } } @@ -1529,7 +1527,7 @@ public void run() { * a chance to clean up before it actually exits. */ private void exit() { - // assert !isLightweight(); + // assert !isVirtual(); if (threadLocals != null && TerminatingThreadLocal.REGISTRY.isPresent()) { TerminatingThreadLocal.threadTerminated(); } @@ -1585,7 +1583,7 @@ private void exit() { * * @throws SecurityException if the current thread cannot * modify this thread. - * @throws UnsupportedOperationException if invoked on a lightweight thread + * @throws UnsupportedOperationException if invoked on a virtual thread * @see #interrupt() * @see #checkAccess() * @see #run() @@ -1602,11 +1600,11 @@ private void exit() { * an inconsistent state, the damaged objects become visible to * other threads, potentially resulting in arbitrary behavior. Many * uses of {@code stop} should be replaced by code that simply - * modifies some variable to indicate that the target thread should - * stop running. The target thread should check this variable + * modifies some variable to indicate that the task thread should + * stop running. The task thread should check this variable * regularly, and return from its run method in an orderly fashion * if the variable indicates that it is to stop running. If the - * target thread waits for long periods (on a condition variable, + * task thread waits for long periods (on a condition variable, * for example), the {@code interrupt} method should be used to * interrupt the wait. * For more information, see @@ -1623,7 +1621,7 @@ public final void stop() { } } - if (isLightweight()) + if (isVirtual()) throw new UnsupportedOperationException(); // A zero status value corresponds to "NEW", it can't change to @@ -1758,7 +1756,7 @@ boolean getAndClearInterrupt() { * {@code false} otherwise. */ public final boolean isAlive() { - if (isLightweight()) { + if (isVirtual()) { State state = getState(); return (state != State.NEW && state != State.TERMINATED); } else { @@ -1779,13 +1777,13 @@ public final boolean isAlive() { * * @throws SecurityException if the current thread cannot modify * this thread. - * @throws UnsupportedOperationException if invoked on a lightweight thread + * @throws UnsupportedOperationException if invoked on a virtual thread * @see #checkAccess * @deprecated This method has been deprecated, as it is - * inherently deadlock-prone. If the target thread holds a lock on the + * inherently deadlock-prone. If the task thread holds a lock on the * monitor protecting a critical system resource when it is suspended, no - * thread can access this resource until the target thread is resumed. If - * the thread that would resume the target thread attempts to lock this + * thread can access this resource until the task thread is resumed. If + * the thread that would resume the task thread attempts to lock this * monitor prior to calling {@code resume}, deadlock results. Such * deadlocks typically manifest themselves as "frozen" processes. * For more information, see @@ -1795,7 +1793,7 @@ public final boolean isAlive() { @Deprecated(since="1.2", forRemoval=true) public final void suspend() { checkAccess(); - if (isLightweight()) + if (isVirtual()) throw new UnsupportedOperationException(); suspend0(); } @@ -1816,7 +1814,7 @@ void suspendThread() { * * @throws SecurityException if the current thread cannot modify this * thread. - * @throws UnsupportedOperationException if invoked on a lightweight thread + * @throws UnsupportedOperationException if invoked on a virtual thread * @see #checkAccess * @see #suspend() * @deprecated This method exists solely for use with {@link #suspend}, @@ -1828,7 +1826,7 @@ void suspendThread() { @Deprecated(since="1.2", forRemoval=true) public final void resume() { checkAccess(); - if (isLightweight()) + if (isVirtual()) throw new UnsupportedOperationException(); resume0(); } @@ -1843,7 +1841,7 @@ void resumeThread() { * First the {@code checkAccess} method of this thread is called * with no arguments. This may result in throwing a {@code SecurityException}. *

- * The priority of lightweight-threads is always {@linkplain Thread#NORM_PRIORITY} + * The priority of virtual-threads is always {@linkplain Thread#NORM_PRIORITY} * and is not changed by this method. * Otherwise, the priority of this thread is set to the smaller of * the specified {@code newPriority} and the maximum permitted @@ -1868,7 +1866,7 @@ public final void setPriority(int newPriority) { if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) { throw new IllegalArgumentException(); } - if (!isLightweight() && (g = getThreadGroup()) != null) { + if (!isVirtual() && (g = getThreadGroup()) != null) { if (newPriority > g.getMaxPriority()) { newPriority = g.getMaxPriority(); } @@ -1878,13 +1876,13 @@ public final void setPriority(int newPriority) { /** * Returns this thread's priority. - * The priority of a lightweight thread is always {@linkplain Thread#NORM_PRIORITY}. + * The priority of a virtual thread is always {@linkplain Thread#NORM_PRIORITY}. * * @return this thread's priority. * @see #setPriority */ public final int getPriority() { - if (isLightweight()) { + if (isVirtual()) { return Thread.NORM_PRIORITY; } else { return holder.priority; @@ -1911,7 +1909,7 @@ public final synchronized void setName(String name) { } this.name = name; - if (!isLightweight() && holder.threadStatus != 0) { + if (!isVirtual() && holder.threadStatus != 0) { setNativeName(name); } } @@ -1930,8 +1928,8 @@ public final String getName() { * Returns the thread group to which this thread belongs. * This method returns null if the thread has terminated. * - *

The thread group for lightweight threads does not support all features - * of regular thread groups. Lightweight threads are not considered active + *

The thread group for virtual threads does not support all features + * of regular thread groups. Virtual threads are not considered active * threads in the thread group and so are not enumerated or acted on by * thread group operations. * @@ -1941,7 +1939,7 @@ public final ThreadGroup getThreadGroup() { if (getState() == State.TERMINATED) { return null; } else { - return isLightweight() ? LightweightThreads.THREAD_GROUP : holder.group; + return isVirtual() ? VirtualThreads.THREAD_GROUP : holder.group; } } @@ -2017,7 +2015,7 @@ public int countStackFrames() { * This method returns immediately, without waiting, if the thread has not * been {@link #start() started}. * - *

For non-{@link #isLightweight() lightweight}, this + *

For non-{@linkplain #isVirtual() virtual}, this * implementation uses a loop of {@code this.wait} calls * conditioned on {@code this.isAlive}. As a thread terminates the * {@code this.notifyAll} method is invoked. It is recommended that @@ -2039,7 +2037,7 @@ public final void join(long millis) throws InterruptedException { if (millis < 0) throw new IllegalArgumentException("timeout value is negative"); - if (isLightweight()) { + if (isVirtual()) { if (isAlive()) { long nanos = TimeUnit.MILLISECONDS.toNanos(millis); ((Fiber) this).joinNanos(nanos); @@ -2072,7 +2070,7 @@ public final void join(long millis) throws InterruptedException { * This method returns immediately, without waiting, if the thread has not * been {@link #start() started}. * - *

For non-{@link #isLightweight() lightweight}, this + *

For non-{@link #isVirtual() virtual}, this * implementation uses a loop of {@code this.wait} calls *

This implementation uses a loop of {@code this.wait} calls * conditioned on {@code this.isAlive}. As a thread terminates the @@ -2161,7 +2159,7 @@ public final boolean join(Duration duration) throws InterruptedException { if (duration.isZero() || duration.isNegative()) return false; - if (isLightweight()) { + if (isVirtual()) { long nanos = TimeUnit.NANOSECONDS.convert(duration); return ((Fiber) this).joinNanos(nanos); } else { @@ -2183,7 +2181,7 @@ public static void dumpStack() { /** * Marks this thread as either a {@linkplain #isDaemon daemon} thread * or a user thread. - * The daemon status of a lightweight thread is meaningless and is not + * The daemon status of a virtual thread is meaningless and is not * changed by this method (the {@linkplain #isDaemon() isDaemon} method * always returns {@code true}). * The Java Virtual Machine exits when the only threads running are all @@ -2205,21 +2203,21 @@ public final void setDaemon(boolean on) { checkAccess(); if (isAlive()) throw new IllegalThreadStateException(); - if (!isLightweight()) + if (!isVirtual()) holder.daemon = on; } /** * Tests if this thread is a daemon thread. - * The daemon status of a lightweight thread is meaningless, this method - * returns {@code true} if this is a lightweight thread. + * The daemon status of a virtual thread is meaningless, this method + * returns {@code true} if this is a virtual thread. * * @return {@code true} if this thread is a daemon thread; * {@code false} otherwise. * @see #setDaemon(boolean) */ public final boolean isDaemon() { - if (isLightweight()) { + if (isVirtual()) { return true; } else { return holder.daemon; @@ -2427,7 +2425,7 @@ public StackTraceElement[] getStackTrace() { /** * Returns a map of stack traces for all live threads. The map does not - * include lightweight threads. + * include virtual threads. * The map keys are threads and each map value is an array of * {@code StackTraceElement} that represents the stack dump * of the corresponding {@code Thread}. @@ -2882,12 +2880,12 @@ public boolean equals(Object obj) { } } - private static class LightweightThreads { + private static class VirtualThreads { static final ThreadGroup THREAD_GROUP = threadGroup(); static final AccessControlContext ACCESS_CONTROL_CONTEXT = accessControlContext(); /** - * The thread group for lightweight threads. + * The thread group for virtual threads. */ private static ThreadGroup threadGroup() { return AccessController.doPrivileged(new PrivilegedAction() { @@ -2895,7 +2893,7 @@ public ThreadGroup run() { ThreadGroup group = Thread.currentCarrierThread().getThreadGroup(); for (ThreadGroup p; (p = group.getParent()) != null; ) group = p; - var newGroup = new ThreadGroup(group, "LightweightThreads", true); + var newGroup = new ThreadGroup(group, "VirtualThreads", true); newGroup.setDaemon(true); return newGroup; }}); diff --git a/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java b/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java index fdae7d1366a..1e0e9714d8e 100644 --- a/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java +++ b/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java @@ -51,7 +51,7 @@ import java.util.function.Predicate; import java.util.concurrent.locks.LockSupport; -import jdk.internal.misc.LightweightThreads; +import jdk.internal.misc.VirtualThreads; /** * An {@link ExecutorService} for running {@link ForkJoinTask}s. @@ -1924,7 +1924,7 @@ private ForkJoinTask externalSubmit(ForkJoinTask task) { Thread t; ForkJoinWorkerThread w; WorkQueue q; if (task == null) throw new NullPointerException(); - if (((t = LightweightThreads.currentCarrierThread()) instanceof ForkJoinWorkerThread) && + if (((t = VirtualThreads.currentCarrierThread()) instanceof ForkJoinWorkerThread) && (w = (ForkJoinWorkerThread)t).pool == this && (q = w.workQueue) != null) q.push(task); diff --git a/src/java.base/share/classes/java/util/concurrent/locks/LockSupport.java b/src/java.base/share/classes/java/util/concurrent/locks/LockSupport.java index 4e383621ef2..a50edca7839 100644 --- a/src/java.base/share/classes/java/util/concurrent/locks/LockSupport.java +++ b/src/java.base/share/classes/java/util/concurrent/locks/LockSupport.java @@ -37,7 +37,7 @@ import java.util.concurrent.TimeUnit; -import jdk.internal.misc.LightweightThreads; +import jdk.internal.misc.VirtualThreads; import jdk.internal.misc.Unsafe; /** @@ -177,8 +177,8 @@ public static void setCurrentBlocker(Object blocker) { */ public static void unpark(Thread thread) { if (thread != null) { - if (thread.isLightweight()) { - LightweightThreads.unpark(thread); // can throw RejectedExecutionException + if (thread.isVirtual()) { + VirtualThreads.unpark(thread); // can throw RejectedExecutionException } else { U.unpark(thread); } @@ -216,8 +216,8 @@ public static void unpark(Thread thread) { public static void park(Object blocker) { Thread t = Thread.currentThread(); setBlocker(t, blocker); - if (t.isLightweight()) { - LightweightThreads.park(); + if (t.isVirtual()) { + VirtualThreads.park(); } else { U.park(false, 0L); } @@ -261,8 +261,8 @@ public static void parkNanos(Object blocker, long nanos) { if (nanos > 0) { Thread t = Thread.currentThread(); setBlocker(t, blocker); - if (t.isLightweight()) { - LightweightThreads.park(nanos); + if (t.isVirtual()) { + VirtualThreads.park(nanos); } else { U.park(false, nanos); } @@ -306,10 +306,10 @@ public static void parkNanos(Object blocker, long nanos) { public static void parkUntil(Object blocker, long deadline) { Thread t = Thread.currentThread(); setBlocker(t, blocker); - if (t.isLightweight()) { + if (t.isVirtual()) { long millis = deadline - System.currentTimeMillis(); long nanos = TimeUnit.NANOSECONDS.convert(millis, TimeUnit.MILLISECONDS); - LightweightThreads.park(nanos); + VirtualThreads.park(nanos); } else { U.park(true, deadline); } @@ -360,8 +360,8 @@ public static Object getBlocker(Thread t) { * for example, the interrupt status of the thread upon return. */ public static void park() { - if (Thread.currentThread().isLightweight()) { - LightweightThreads.park(); + if (Thread.currentThread().isVirtual()) { + VirtualThreads.park(); } else { U.park(false, 0L); } @@ -399,8 +399,8 @@ public static void park() { */ public static void parkNanos(long nanos) { if (nanos > 0) { - if (Thread.currentThread().isLightweight()) { - LightweightThreads.park(nanos); + if (Thread.currentThread().isVirtual()) { + VirtualThreads.park(nanos); } else { U.park(false, nanos); } @@ -438,10 +438,10 @@ public static void parkNanos(long nanos) { * to wait until */ public static void parkUntil(long deadline) { - if (Thread.currentThread().isLightweight()) { + if (Thread.currentThread().isVirtual()) { long millis = deadline - System.currentTimeMillis(); long nanos = TimeUnit.NANOSECONDS.convert(millis, TimeUnit.MILLISECONDS); - LightweightThreads.park(nanos); + VirtualThreads.park(nanos); } else { U.park(true, deadline); } diff --git a/src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java b/src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java index cc081acd229..e07c365a5a2 100644 --- a/src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java +++ b/src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java @@ -343,17 +343,17 @@ public interface JavaLangAccess { void setCarrierThreadLocal(ThreadLocal local, T value); /** - * Parks the current lightweight thread. + * Parks the current virtual thread. */ - void parkLightweightThread(); + void parkVirtualThread(); /** - * Parks the current lightweight thread.for up to the given waiting time. + * Parks the current virtual thread.for up to the given waiting time. */ - void parkLightweightThread(long nanos); + void parkVirtualThread(long nanos); /** - * Unparks the given lightweight thread. + * Unparks the given virtual thread. */ - void unparkLightweightThread(Thread thread); + void unparkVirtualThread(Thread thread); } diff --git a/src/java.base/share/classes/jdk/internal/math/FloatingDecimal.java b/src/java.base/share/classes/jdk/internal/math/FloatingDecimal.java index 5f849a1ee27..0c9243d9903 100644 --- a/src/java.base/share/classes/jdk/internal/math/FloatingDecimal.java +++ b/src/java.base/share/classes/jdk/internal/math/FloatingDecimal.java @@ -983,7 +983,7 @@ protected BinaryToASCIIBuffer initialValue() { }; private static BinaryToASCIIBuffer getBinaryToASCIIBuffer() { - if (Thread.currentThread().isLightweight()) { + if (Thread.currentThread().isVirtual()) { return new BinaryToASCIIBuffer(); } else { return threadLocalBinaryToASCIIBuffer.get(); diff --git a/src/java.base/share/classes/jdk/internal/math/FormattedFloatingDecimal.java b/src/java.base/share/classes/jdk/internal/math/FormattedFloatingDecimal.java index 5c1293184df..c2d8c169201 100644 --- a/src/java.base/share/classes/jdk/internal/math/FormattedFloatingDecimal.java +++ b/src/java.base/share/classes/jdk/internal/math/FormattedFloatingDecimal.java @@ -51,7 +51,7 @@ protected char[] initialValue() { }; private static char[] getBuffer(){ - if (Thread.currentThread().isLightweight()) { + if (Thread.currentThread().isVirtual()) { return new char[20]; } else { return threadLocalCharBuffer.get(); diff --git a/src/java.base/share/classes/jdk/internal/misc/Blocker.java b/src/java.base/share/classes/jdk/internal/misc/Blocker.java index f08f892d960..03b0696376a 100644 --- a/src/java.base/share/classes/jdk/internal/misc/Blocker.java +++ b/src/java.base/share/classes/jdk/internal/misc/Blocker.java @@ -50,7 +50,7 @@ public interface BlockingCallable { } public static void runBlocking(BlockingRunnable task) throws X { - if (Thread.currentThread().isLightweight()) { + if (Thread.currentThread().isVirtual()) { Callable wrapper = () -> { try { task.run(); @@ -66,7 +66,7 @@ public static void runBlocking(BlockingRunnable task) t } public static V runBlocking(BlockingCallable task) throws X { - if (Thread.currentThread().isLightweight()) { + if (Thread.currentThread().isVirtual()) { Callable wrapper = () -> { try { return task.call(); diff --git a/src/java.base/share/classes/jdk/internal/misc/TerminatingThreadLocal.java b/src/java.base/share/classes/jdk/internal/misc/TerminatingThreadLocal.java index c9d8b8497be..95cc82008b2 100644 --- a/src/java.base/share/classes/jdk/internal/misc/TerminatingThreadLocal.java +++ b/src/java.base/share/classes/jdk/internal/misc/TerminatingThreadLocal.java @@ -79,7 +79,7 @@ public static void threadTerminated() { * @param tl the ThreadLocal to register */ public static void register(TerminatingThreadLocal tl) { - if (!Thread.currentThread().isLightweight()) + if (!Thread.currentThread().isVirtual()) REGISTRY.get().add(tl); } @@ -89,7 +89,7 @@ public static void register(TerminatingThreadLocal tl) { * @param tl the ThreadLocal to unregister */ private static void unregister(TerminatingThreadLocal tl) { - if (!Thread.currentThread().isLightweight()) + if (!Thread.currentThread().isVirtual()) REGISTRY.get().remove(tl); } diff --git a/src/java.base/share/classes/jdk/internal/misc/LightweightThreads.java b/src/java.base/share/classes/jdk/internal/misc/VirtualThreads.java similarity index 81% rename from src/java.base/share/classes/jdk/internal/misc/LightweightThreads.java rename to src/java.base/share/classes/jdk/internal/misc/VirtualThreads.java index 651c581e409..95ba99b2649 100644 --- a/src/java.base/share/classes/jdk/internal/misc/LightweightThreads.java +++ b/src/java.base/share/classes/jdk/internal/misc/VirtualThreads.java @@ -29,10 +29,10 @@ import jdk.internal.access.SharedSecrets; /** - * Supporting methods to park/unpark lightweight threads. + * Supporting methods to park/unpark virtual threads. */ -public final class LightweightThreads { +public final class VirtualThreads { private static final JavaLangAccess JLA; static { JLA = SharedSecrets.getJavaLangAccess(); @@ -40,7 +40,7 @@ public final class LightweightThreads { throw new InternalError("JavaLangAccess not setup"); } } - private LightweightThreads() { } + private VirtualThreads() { } /** * Returns the current carrier thread @@ -50,23 +50,23 @@ public static Thread currentCarrierThread() { } /** - * Parks the current lightweight thread + * Parks the current virtual thread */ public static void park() { - JLA.parkLightweightThread(); + JLA.parkVirtualThread(); } /** - * Parks the current lightweight thread for up to the given waiting time + * Parks the current virtual thread for up to the given waiting time */ public static void park(long nanos) { - JLA.parkLightweightThread(nanos); + JLA.parkVirtualThread(nanos); } /** - * Unparks the given lightweight thread + * Unparks the given virtual thread */ public static void unpark(Thread thread) { - JLA.unparkLightweightThread(thread); + JLA.unparkVirtualThread(thread); } } diff --git a/src/java.base/share/classes/jdk/internal/reflect/NativeConstructorAccessorImpl.java b/src/java.base/share/classes/jdk/internal/reflect/NativeConstructorAccessorImpl.java index 1195573fe09..b12d31658ee 100644 --- a/src/java.base/share/classes/jdk/internal/reflect/NativeConstructorAccessorImpl.java +++ b/src/java.base/share/classes/jdk/internal/reflect/NativeConstructorAccessorImpl.java @@ -50,7 +50,7 @@ public Object newInstance(Object[] args) // because that kind of class can't be referred to by name, hence can't // be found from the generated bytecode. if (VM.isBooted() && (++numInvocations > ReflectionFactory.inflationThreshold() - || Thread.currentThread().isLightweight()) + || Thread.currentThread().isVirtual()) && !ReflectUtil.isVMAnonymousClass(c.getDeclaringClass())) { ConstructorAccessorImpl acc = (ConstructorAccessorImpl) new MethodAccessorGenerator(). diff --git a/src/java.base/share/classes/jdk/internal/reflect/NativeMethodAccessorImpl.java b/src/java.base/share/classes/jdk/internal/reflect/NativeMethodAccessorImpl.java index d80d3e7a860..eb3c93cd868 100644 --- a/src/java.base/share/classes/jdk/internal/reflect/NativeMethodAccessorImpl.java +++ b/src/java.base/share/classes/jdk/internal/reflect/NativeMethodAccessorImpl.java @@ -48,7 +48,7 @@ public Object invoke(Object obj, Object[] args) // that kind of class can't be referred to by name, hence can't be // found from the generated bytecode. if (VM.isBooted() && (++numInvocations > ReflectionFactory.inflationThreshold() - || Thread.currentThread().isLightweight()) + || Thread.currentThread().isVirtual()) && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) { MethodAccessorImpl acc = (MethodAccessorImpl) new MethodAccessorGenerator(). diff --git a/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java b/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java index 50b04a9d55b..a2f4e5634f6 100644 --- a/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java @@ -997,7 +997,7 @@ private boolean tryLockedConfigureBlocking(boolean block) throws IOException { */ private void lockedConfigureNonBlockingIfNeeded() throws IOException { assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread(); - if (!nonBlocking && Thread.currentThread().isLightweight()) { + if (!nonBlocking && Thread.currentThread().isVirtual()) { synchronized (stateLock) { ensureOpen(); IOUtil.configureBlocking(fd, false); @@ -1449,8 +1449,8 @@ private void implCloseBlockingMode() throws IOException { long reader = readerThread; long writer = writerThread; if (reader != 0 || writer != 0) { - if (NativeThread.isLightweightThread(reader) - || NativeThread.isLightweightThread(writer)) { + if (NativeThread.isVirtualThread(reader) + || NativeThread.isVirtualThread(writer)) { Poller.stopPoll(fdVal); } nd.preClose(fd); diff --git a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java index be33d53f1ac..b806c636f15 100644 --- a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java @@ -50,7 +50,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; -import jdk.internal.misc.LightweightThreads; +import jdk.internal.misc.VirtualThreads; import jdk.internal.ref.CleanerFactory; import jdk.internal.access.SharedSecrets; import sun.net.ConnectionResetException; @@ -173,15 +173,15 @@ private void ensureOpenAndConnected() throws SocketException { */ private void park(FileDescriptor fd, int event, long nanos) throws IOException { Thread t = Thread.currentThread(); - if (t.isLightweight()) { + if (t.isVirtual()) { int fdVal = fdVal(fd); Poller.register(fdVal, event); if (isOpen()) { try { if (nanos == 0) { - LightweightThreads.park(); + VirtualThreads.park(); } else { - LightweightThreads.park(nanos); + VirtualThreads.park(nanos); } // throw SocketException with interrupt status set for now if (t.isInterrupted()) { @@ -220,7 +220,7 @@ private void configureNonBlockingIfNeeded(FileDescriptor fd, boolean timed) throws IOException { if (!nonBlocking - && (timed || Thread.currentThread().isLightweight())) { + && (timed || Thread.currentThread().isVirtual())) { assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread(); IOUtil.configureBlocking(fd, false); nonBlocking = true; @@ -901,8 +901,8 @@ protected void close() throws IOException { if (!tryClose()) { long reader = readerThread; long writer = writerThread; - if (NativeThread.isLightweightThread(reader) - || NativeThread.isLightweightThread(writer)) + if (NativeThread.isVirtualThread(reader) + || NativeThread.isVirtualThread(writer)) Poller.stopPoll(fdVal(fd)); nd.preClose(fd); if (NativeThread.isKernelThread(reader)) @@ -1149,7 +1149,7 @@ protected void shutdownInput() throws IOException { ensureOpenAndConnected(); if (!isInputClosed) { Net.shutdown(fd, Net.SHUT_RD); - if (NativeThread.isLightweightThread(readerThread)) { + if (NativeThread.isVirtualThread(readerThread)) { Poller.stopPoll(fdVal(fd), Net.POLLIN); } isInputClosed = true; @@ -1163,7 +1163,7 @@ protected void shutdownOutput() throws IOException { ensureOpenAndConnected(); if (!isOutputClosed) { Net.shutdown(fd, Net.SHUT_WR); - if (NativeThread.isLightweightThread(writerThread)) { + if (NativeThread.isVirtualThread(writerThread)) { Poller.stopPoll(fdVal(fd), Net.POLLOUT); } isOutputClosed = true; diff --git a/src/java.base/share/classes/sun/nio/ch/SelChImpl.java b/src/java.base/share/classes/sun/nio/ch/SelChImpl.java index 0a1f9d1eb0b..82c38f3337e 100644 --- a/src/java.base/share/classes/sun/nio/ch/SelChImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/SelChImpl.java @@ -31,7 +31,7 @@ import static java.util.concurrent.TimeUnit.NANOSECONDS; -import jdk.internal.misc.LightweightThreads; +import jdk.internal.misc.VirtualThreads; /** * An interface that allows translation (and more!). @@ -85,14 +85,14 @@ public interface SelChImpl extends Channel { * @param nanos the timeout to wait; {@code <= 0} to wait indefinitely */ default void park(int event, long nanos) throws IOException { - if (Thread.currentThread().isLightweight()) { + if (Thread.currentThread().isVirtual()) { Poller.register(getFDVal(), event); if (isOpen()) { try { if (nanos == 0) { - LightweightThreads.park(); + VirtualThreads.park(); } else { - LightweightThreads.park(nanos); + VirtualThreads.park(nanos); } } finally { Poller.deregister(getFDVal(), event); diff --git a/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java b/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java index 5431cf141ad..f7d112da1cf 100644 --- a/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java @@ -416,7 +416,7 @@ private boolean tryLockedConfigureBlocking(boolean block) throws IOException { */ private void lockedConfigureNonBlockingIfNeeded() throws IOException { assert acceptLock.isHeldByCurrentThread(); - if (!nonBlocking && (Thread.currentThread().isLightweight())) { + if (!nonBlocking && (Thread.currentThread().isVirtual())) { synchronized (stateLock) { ensureOpen(); IOUtil.configureBlocking(fd, false); @@ -465,7 +465,7 @@ private void implCloseBlockingMode() throws IOException { if (!tryClose()) { long th = thread; if (th != 0) { - if (NativeThread.isLightweightThread(th)) + if (NativeThread.isVirtualThread(th)) Poller.stopPoll(fdVal); nd.preClose(fd); if (NativeThread.isKernelThread(th)) diff --git a/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java b/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java index 288d37a19ba..113a8d6cd20 100644 --- a/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java @@ -616,7 +616,7 @@ private boolean tryLockedConfigureBlocking(boolean block) throws IOException { */ private void configureNonBlockingIfNeeded() throws IOException { assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread(); - if (!nonBlocking && Thread.currentThread().isLightweight()) { + if (!nonBlocking && Thread.currentThread().isVirtual()) { synchronized (stateLock) { ensureOpen(); IOUtil.configureBlocking(fd, false); @@ -931,8 +931,8 @@ private void implCloseBlockingMode() throws IOException { long reader = readerThread; long writer = writerThread; if (reader != 0 || writer != 0) { - if (NativeThread.isLightweightThread(reader) - || NativeThread.isLightweightThread(writer)) { + if (NativeThread.isVirtualThread(reader) + || NativeThread.isVirtualThread(writer)) { Poller.stopPoll(fdVal); } nd.preClose(fd); @@ -1020,7 +1020,7 @@ public SocketChannel shutdownInput() throws IOException { if (!isInputClosed) { Net.shutdown(fd, Net.SHUT_RD); long reader = readerThread; - if (NativeThread.isLightweightThread(reader)) { + if (NativeThread.isVirtualThread(reader)) { Poller.stopPoll(fdVal, Net.POLLIN); } else if (NativeThread.isKernelThread(reader)) { NativeThread.signal(reader); @@ -1040,7 +1040,7 @@ public SocketChannel shutdownOutput() throws IOException { if (!isOutputClosed) { Net.shutdown(fd, Net.SHUT_WR); long writer = writerThread; - if (NativeThread.isLightweightThread(writer)) { + if (NativeThread.isVirtualThread(writer)) { Poller.stopPoll(fdVal, Net.POLLOUT); } else if (NativeThread.isKernelThread(writer)) { NativeThread.signal(writer); diff --git a/src/java.base/share/classes/sun/nio/cs/ThreadLocalCoders.java b/src/java.base/share/classes/sun/nio/cs/ThreadLocalCoders.java index 9546cd7bb2d..4bf4dffbc4d 100644 --- a/src/java.base/share/classes/sun/nio/cs/ThreadLocalCoders.java +++ b/src/java.base/share/classes/sun/nio/cs/ThreadLocalCoders.java @@ -60,7 +60,7 @@ private void moveToFront(Object[] oa, int i) { Object forName(Object name) { Object[] oa; - if (Thread.currentThread().isLightweight()) { + if (Thread.currentThread().isVirtual()) { oa = new Object[size]; } else { oa = cache.get(); diff --git a/src/java.base/unix/classes/sun/nio/ch/NativeThread.java b/src/java.base/unix/classes/sun/nio/ch/NativeThread.java index 576c9dcea50..8cb7eae594d 100644 --- a/src/java.base/unix/classes/sun/nio/ch/NativeThread.java +++ b/src/java.base/unix/classes/sun/nio/ch/NativeThread.java @@ -37,15 +37,15 @@ // always returns -1 and the signal(long) method has no effect. public class NativeThread { - private static final long LIGHTWEIGHT_THREAD_ID = -1L; + private static final long VIRTUAL_THREAD_ID = -1L; /** * Returns a token representing the current thread or -1 if called in the - * context of a lightweight thread + * context of a virtual thread */ public static long current() { - if (Thread.currentThread().isLightweight()) { - return LIGHTWEIGHT_THREAD_ID; + if (Thread.currentThread().isVirtual()) { + return VIRTUAL_THREAD_ID; } else { return current0(); } @@ -64,23 +64,23 @@ static long currentKernelThread() { * @throws IllegalArgumentException if tid is not a token to a kernel thread */ public static void signal(long tid) { - if (tid == 0 || tid == LIGHTWEIGHT_THREAD_ID) + if (tid == 0 || tid == VIRTUAL_THREAD_ID) throw new IllegalArgumentException(); signal0(tid); } /** - * Returns true if the token presents a lightweight thread + * Returns true if the token presents a virtual thread */ - static boolean isLightweightThread(long tid) { - return (tid == LIGHTWEIGHT_THREAD_ID); + static boolean isVirtualThread(long tid) { + return (tid == VIRTUAL_THREAD_ID); } /** * Returns true if the token presents a kernel thread */ static boolean isKernelThread(long tid) { - return (tid != 0 && tid != LIGHTWEIGHT_THREAD_ID); + return (tid != 0 && tid != VIRTUAL_THREAD_ID); } // Returns an opaque token representing the native thread underlying the diff --git a/src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java b/src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java index 7236749b68a..7248a11bd9f 100644 --- a/src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java +++ b/src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java @@ -124,7 +124,7 @@ private void implCloseBlockingMode() throws IOException { if (!tryClose()) { long th = thread; if (th != 0) { - if (NativeThread.isLightweightThread(th)) + if (NativeThread.isVirtualThread(th)) Poller.stopPoll(fdVal); nd.preClose(fd); if (NativeThread.isKernelThread(th)) diff --git a/src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java b/src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java index 78f2fc7c862..e9d356270b7 100644 --- a/src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java +++ b/src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java @@ -124,7 +124,7 @@ private void implCloseBlockingMode() throws IOException { if (!tryClose()) { long th = thread; if (th != 0) { - if (NativeThread.isLightweightThread(th)) + if (NativeThread.isVirtualThread(th)) Poller.stopPoll(fdVal); nd.preClose(fd); if (NativeThread.isKernelThread(th)) diff --git a/src/java.base/windows/classes/sun/nio/ch/NativeThread.java b/src/java.base/windows/classes/sun/nio/ch/NativeThread.java index 18764c05d1b..81491378425 100644 --- a/src/java.base/windows/classes/sun/nio/ch/NativeThread.java +++ b/src/java.base/windows/classes/sun/nio/ch/NativeThread.java @@ -29,11 +29,11 @@ // Signalling operations on native threads class NativeThread { - private static final long LIGHTWEIGHT_THREAD_ID = -1L; + private static final long VIRTUAL_THREAD_ID = -1L; static long current() { - if (Thread.currentThread().isLightweight()) { - return LIGHTWEIGHT_THREAD_ID; + if (Thread.currentThread().isVirtual()) { + return VIRTUAL_THREAD_ID; } else { return 0; } @@ -48,7 +48,7 @@ static void signal(long tid) { } static boolean isLightweightThread(long tid) { - return (tid == LIGHTWEIGHT_THREAD_ID); + return (tid == VIRTUAL_THREAD_ID); } static boolean isKernelThread(long tid) { diff --git a/test/hotspot/jtreg/serviceability/jvmti/DoContinueSingleStepTest/DoContinueSingleStepTest.java b/test/hotspot/jtreg/serviceability/jvmti/DoContinueSingleStepTest/DoContinueSingleStepTest.java index 05e9828fdcc..b7bbffef60c 100644 --- a/test/hotspot/jtreg/serviceability/jvmti/DoContinueSingleStepTest/DoContinueSingleStepTest.java +++ b/test/hotspot/jtreg/serviceability/jvmti/DoContinueSingleStepTest/DoContinueSingleStepTest.java @@ -65,9 +65,9 @@ public class DoContinueSingleStepTest { }; public static void test1() throws Exception { - Thread p1 = Thread.newThread(Thread.LIGHTWEIGHT, PRODUCER); - Thread c1 = Thread.newThread(Thread.LIGHTWEIGHT, CONSUMER); - Thread c2 = Thread.newThread(Thread.LIGHTWEIGHT, CONSUMER); + Thread p1 = Thread.newThread(Thread.VIRTUAL, PRODUCER); + Thread c1 = Thread.newThread(Thread.VIRTUAL, CONSUMER); + Thread c2 = Thread.newThread(Thread.VIRTUAL, CONSUMER); p1.start(); c1.start(); c2.start(); diff --git a/test/hotspot/jtreg/serviceability/jvmti/FiberTest/MyPackage/FiberTest.java b/test/hotspot/jtreg/serviceability/jvmti/FiberTest/MyPackage/FiberTest.java index 67b00494ae0..c26c465b67e 100644 --- a/test/hotspot/jtreg/serviceability/jvmti/FiberTest/MyPackage/FiberTest.java +++ b/test/hotspot/jtreg/serviceability/jvmti/FiberTest/MyPackage/FiberTest.java @@ -65,8 +65,8 @@ static void producer(String msg) throws InterruptedException { }; public static void test1() throws Exception { - Thread prod = Thread.newThread(Thread.LIGHTWEIGHT, PRODUCER); - Thread cons = Thread.newThread(Thread.LIGHTWEIGHT, CONSUMER); + Thread prod = Thread.newThread(Thread.VIRTUAL, PRODUCER); + Thread cons = Thread.newThread(Thread.VIRTUAL, CONSUMER); prod.start(); cons.start(); prod.join(); diff --git a/test/jdk/java/lang/Thread/BuilderTest.java b/test/jdk/java/lang/Thread/BuilderTest.java index f6f97a07707..a06362d3299 100644 --- a/test/jdk/java/lang/Thread/BuilderTest.java +++ b/test/jdk/java/lang/Thread/BuilderTest.java @@ -44,8 +44,8 @@ public void testKernelThread1() throws Exception { // build AtomicBoolean done1 = new AtomicBoolean(); - Thread thread1 = builder.target(() -> done1.set(true)).build(); - assertFalse(thread1.isLightweight()); + Thread thread1 = builder.task(() -> done1.set(true)).build(); + assertFalse(thread1.isVirtual()); assertTrue(thread1.getState() == Thread.State.NEW); assertFalse(thread1.getName().isEmpty()); assertTrue(thread1.getThreadGroup() == parent.getThreadGroup()); @@ -58,8 +58,8 @@ public void testKernelThread1() throws Exception { // start AtomicBoolean done2 = new AtomicBoolean(); - Thread thread2 = builder.target(() -> done2.set(true)).start(); - assertFalse(thread2.isLightweight()); + Thread thread2 = builder.task(() -> done2.set(true)).start(); + assertFalse(thread2.isVirtual()); assertTrue(thread2.getState() != Thread.State.NEW); assertFalse(thread2.getName().isEmpty()); assertTrue(thread2.getThreadGroup() == parent.getThreadGroup()); @@ -72,7 +72,7 @@ public void testKernelThread1() throws Exception { // factory AtomicBoolean done3 = new AtomicBoolean(); Thread thread3 = builder.factory().newThread(() -> done3.set(true)); - assertFalse(thread3.isLightweight()); + assertFalse(thread3.isVirtual()); assertTrue(thread3.getState() == Thread.State.NEW); assertFalse(thread3.getName().isEmpty()); assertTrue(thread3.getThreadGroup() == parent.getThreadGroup()); @@ -87,12 +87,12 @@ public void testKernelThread1() throws Exception { // virtual thread public void testVirtualThread1() throws Exception { Thread parent = Thread.currentThread(); - Thread.Builder builder = Thread.builder().lightweight(); + Thread.Builder builder = Thread.builder().virtual(); // build AtomicBoolean done1 = new AtomicBoolean(); - Thread thread1 = builder.target(() -> done1.set(true)).build(); - assertTrue(thread1.isLightweight()); + Thread thread1 = builder.task(() -> done1.set(true)).build(); + assertTrue(thread1.isVirtual()); assertTrue(thread1.getState() == Thread.State.NEW); assertTrue(thread1.getName().isEmpty()); assertTrue(thread1.getContextClassLoader() == parent.getContextClassLoader()); @@ -104,8 +104,8 @@ public void testVirtualThread1() throws Exception { // start AtomicBoolean done2 = new AtomicBoolean(); - Thread thread2 = builder.target(() -> done2.set(true)).start(); - assertTrue(thread2.isLightweight()); + Thread thread2 = builder.task(() -> done2.set(true)).start(); + assertTrue(thread2.isVirtual()); assertTrue(thread2.getState() != Thread.State.NEW); assertTrue(thread2.getName().isEmpty()); assertTrue(thread2.getContextClassLoader() == parent.getContextClassLoader()); @@ -117,7 +117,7 @@ public void testVirtualThread1() throws Exception { // factory AtomicBoolean done3 = new AtomicBoolean(); Thread thread3 = builder.factory().newThread(() -> done3.set(true)); - assertTrue(thread3.isLightweight()); + assertTrue(thread3.isVirtual()); assertTrue(thread3.getState() == Thread.State.NEW); assertTrue(thread3.getName().isEmpty()); assertTrue(thread3.getContextClassLoader() == parent.getContextClassLoader()); @@ -132,8 +132,8 @@ public void testVirtualThread1() throws Exception { public void testName1() { Thread.Builder builder = Thread.builder().name("duke"); - Thread thread1 = builder.target(() -> { }).build(); - Thread thread2 = builder.target(() -> { }).start(); + Thread thread1 = builder.task(() -> { }).build(); + Thread thread2 = builder.task(() -> { }).start(); Thread thread3 = builder.factory().newThread(() -> { }); assertTrue(thread1.getName().equals("duke")); @@ -142,10 +142,10 @@ public void testName1() { } public void testName2() { - Thread.Builder builder = Thread.builder().lightweight().name("duke"); + Thread.Builder builder = Thread.builder().virtual().name("duke"); - Thread thread1 = builder.target(() -> { }).build(); - Thread thread2 = builder.target(() -> { }).start(); + Thread thread1 = builder.task(() -> { }).build(); + Thread thread2 = builder.task(() -> { }).start(); Thread thread3 = builder.factory().newThread(() -> { }); assertTrue(thread1.getName().equals("duke")); @@ -156,9 +156,9 @@ public void testName2() { public void testName3() { Thread.Builder builder = Thread.builder().name("duke-", 100); - Thread thread1 = builder.target(() -> { }).build(); - Thread thread2 = builder.target(() -> { }).build(); - Thread thread3 = builder.target(() -> { }).build(); + Thread thread1 = builder.task(() -> { }).build(); + Thread thread2 = builder.task(() -> { }).build(); + Thread thread3 = builder.task(() -> { }).build(); assertTrue(thread1.getName().equals("duke-100")); assertTrue(thread2.getName().equals("duke-101")); @@ -175,11 +175,11 @@ public void testName3() { } public void testName4() { - Thread.Builder builder = Thread.builder().lightweight().name("duke-", 100); + Thread.Builder builder = Thread.builder().virtual().name("duke-", 100); - Thread thread1 = builder.target(() -> { }).build(); - Thread thread2 = builder.target(() -> { }).build(); - Thread thread3 = builder.target(() -> { }).build(); + Thread thread1 = builder.task(() -> { }).build(); + Thread thread2 = builder.task(() -> { }).build(); + Thread thread3 = builder.task(() -> { }).build(); assertTrue(thread1.getName().equals("duke-100")); assertTrue(thread2.getName().equals("duke-101")); @@ -200,10 +200,10 @@ public void testThreadGroup1() { ThreadGroup group = new ThreadGroup("groupies"); Thread.Builder builder = Thread.builder().group(group); - Thread thread1 = builder.target(() -> { }).build(); + Thread thread1 = builder.task(() -> { }).build(); AtomicBoolean done = new AtomicBoolean(); - Thread thread2 = builder.target(() -> { + Thread thread2 = builder.task(() -> { while (!done.get()) { LockSupport.park(); } @@ -223,11 +223,11 @@ public void testThreadGroup1() { public void testThreadGroup2() { ThreadGroup group = new ThreadGroup("groupies"); - Thread.Builder builder1 = Thread.builder().lightweight(); + Thread.Builder builder1 = Thread.builder().virtual(); assertThrows(IllegalStateException.class, () -> builder1.group(group)); Thread.Builder builder2 = Thread.builder().group(group); - assertThrows(IllegalStateException.class, () -> builder2.lightweight()); + assertThrows(IllegalStateException.class, () -> builder2.virtual()); } @@ -247,14 +247,14 @@ public void testExecutor() throws Exception { }); Thread.Builder builder = Thread.builder() - .lightweight() - .executor(wrapper::execute); + .virtual() + .scheduler(wrapper::execute); - Thread thread1 = builder.target(() -> { }).build(); + Thread thread1 = builder.task(() -> { }).build(); thread1.start(); thread1.join(); - Thread thread2 = builder.target(() -> { }).start(); + Thread thread2 = builder.task(() -> { }).start(); thread2.join(); Thread thread3 = builder.factory().newThread(() -> { }); @@ -273,8 +273,8 @@ public void testPriority1() { int priority = Thread.currentThread().getThreadGroup().getMaxPriority(); Thread.Builder builder = Thread.builder().priority(priority); - Thread thread1 = builder.target(() -> { }).build(); - Thread thread2 = builder.target(() -> { }).start(); + Thread thread1 = builder.task(() -> { }).build(); + Thread thread2 = builder.task(() -> { }).start(); Thread thread3 = builder.factory().newThread(() -> { }); assertTrue(thread1.getPriority() == priority); @@ -283,10 +283,10 @@ public void testPriority1() { } public void testPriority2() { - Thread.Builder builder = Thread.builder().lightweight().priority(Thread.MAX_PRIORITY); + Thread.Builder builder = Thread.builder().virtual().priority(Thread.MAX_PRIORITY); - Thread thread1 = builder.target(() -> { }).build(); - Thread thread2 = builder.target(() -> { }).start(); + Thread thread1 = builder.task(() -> { }).build(); + Thread thread2 = builder.task(() -> { }).start(); Thread thread3 = builder.factory().newThread(() -> { }); assertTrue(thread1.getPriority() == Thread.NORM_PRIORITY); @@ -298,8 +298,8 @@ public void testPriority2() { public void testDaemon1() { Thread.Builder builder = Thread.builder().daemon(false); - Thread thread1 = builder.target(() -> { }).build(); - Thread thread2 = builder.target(() -> { }).start(); + Thread thread1 = builder.task(() -> { }).build(); + Thread thread2 = builder.task(() -> { }).start(); Thread thread3 = builder.factory().newThread(() -> { }); assertFalse(thread1.isDaemon()); @@ -310,8 +310,8 @@ public void testDaemon1() { public void testDaemon2() { Thread.Builder builder = Thread.builder().daemon(true); - Thread thread1 = builder.target(() -> { }).build(); - Thread thread2 = builder.target(() -> { }).start(); + Thread thread1 = builder.task(() -> { }).build(); + Thread thread2 = builder.task(() -> { }).start(); Thread thread3 = builder.factory().newThread(() -> { }); assertTrue(thread1.isDaemon()); @@ -320,10 +320,10 @@ public void testDaemon2() { } public void testDaemo3() { - Thread.Builder builder = Thread.builder().lightweight().daemon(false); + Thread.Builder builder = Thread.builder().virtual().daemon(false); - Thread thread1 = builder.target(() -> { }).build(); - Thread thread2 = builder.target(() -> { }).start(); + Thread thread1 = builder.task(() -> { }).build(); + Thread thread2 = builder.task(() -> { }).start(); Thread thread3 = builder.factory().newThread(() -> { }); assertTrue(thread1.isDaemon()); @@ -347,13 +347,13 @@ private void testThreadLocals(Thread.Builder builder) throws Exception { }; done.set(false); - Thread thread1 = builder.target(task).build(); + Thread thread1 = builder.task(task).build(); thread1.start(); thread1.join(); assertTrue(done.get()); done.set(false); - Thread thread2 = builder.target(task).start(); + Thread thread2 = builder.task(task).start(); thread2.join(); assertTrue(done.get()); @@ -385,13 +385,13 @@ private void testNoThreadLocals(Thread.Builder builder) throws Exception { }; done.set(false); - Thread thread1 = builder.target(task).build(); + Thread thread1 = builder.task(task).build(); thread1.start(); thread1.join(); assertTrue(done.get()); done.set(false); - Thread thread2 = builder.target(task).start(); + Thread thread2 = builder.task(task).start(); thread2.join(); assertTrue(done.get()); @@ -409,7 +409,7 @@ public void testThreadLocals1() throws Exception { } public void testThreadLocals2() throws Exception { - Thread.Builder builder = Thread.builder().lightweight(); + Thread.Builder builder = Thread.builder().virtual(); testThreadLocals(builder); } @@ -419,7 +419,7 @@ public void testThreadLocals3() throws Exception { } public void testThreadLocals4() throws Exception { - Thread.Builder builder = Thread.builder().lightweight().disallowThreadLocals(); + Thread.Builder builder = Thread.builder().virtual().disallowThreadLocals(); testNoThreadLocals(builder); } @@ -437,13 +437,13 @@ private void testInheritedThreadLocals(Thread.Builder builder) throws Exception }; done.set(false); - Thread thread1 = builder.target(task).build(); + Thread thread1 = builder.task(task).build(); thread1.start(); thread1.join(); assertTrue(done.get()); done.set(false); - Thread thread2 = builder.target(task).start(); + Thread thread2 = builder.task(task).start(); thread2.join(); assertTrue(done.get()); @@ -466,13 +466,13 @@ private void testNoInheritedThreadLocals(Thread.Builder builder) throws Exceptio }; done.set(false); - Thread thread1 = builder.target(task).build(); + Thread thread1 = builder.task(task).build(); thread1.start(); thread1.join(); assertTrue(done.get()); done.set(false); - Thread thread2 = builder.target(task).start(); + Thread thread2 = builder.task(task).start(); thread2.join(); assertTrue(done.get()); @@ -490,7 +490,7 @@ public void testInheritedThreadLocals1() throws Exception { } public void testInheritedThreadLocals2() throws Exception { - Thread.Builder builder = Thread.builder().lightweight().inheritThreadLocals(); + Thread.Builder builder = Thread.builder().virtual().inheritThreadLocals(); testInheritedThreadLocals(builder); } @@ -500,7 +500,7 @@ public void testInheritedThreadLocals3() throws Exception { } public void testInheritedThreadLocals4() throws Exception { - Thread.Builder builder = Thread.builder().lightweight(); + Thread.Builder builder = Thread.builder().virtual(); testNoInheritedThreadLocals(builder); } @@ -508,8 +508,8 @@ public void testInheritedThreadLocals4() throws Exception { public void testNulls() { Thread.Builder builder = Thread.builder(); assertThrows(NullPointerException.class, () -> builder.group(null)); - assertThrows(NullPointerException.class, () -> builder.executor(null)); + assertThrows(NullPointerException.class, () -> builder.scheduler(null)); assertThrows(NullPointerException.class, () -> builder.name(null)); - assertThrows(NullPointerException.class, () -> builder.target(null)); + assertThrows(NullPointerException.class, () -> builder.task(null)); } } diff --git a/test/jdk/java/lang/Thread/lightweight/Collectable.java b/test/jdk/java/lang/Thread/virtual/Collectable.java similarity index 81% rename from test/jdk/java/lang/Thread/lightweight/Collectable.java rename to test/jdk/java/lang/Thread/virtual/Collectable.java index d191a268973..bbfc4035833 100644 --- a/test/jdk/java/lang/Thread/lightweight/Collectable.java +++ b/test/jdk/java/lang/Thread/virtual/Collectable.java @@ -24,7 +24,7 @@ /** * @test * @run testng Collectable - * @summary Test that lightweight threads are GC'ed + * @summary Test that virtual threads are GC'ed */ import java.lang.ref.WeakReference; @@ -36,26 +36,26 @@ @Test public class Collectable { - // ensure that an unstarted lightweight thread can be GC"ed + // ensure that an unstarted virtual thread can be GC"ed public void testGC1() { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { }); + var thread = Thread.newThread(Thread.VIRTUAL, () -> { }); var ref = new WeakReference<>(thread); thread = null; waitUntilCleared(ref); } - // ensure that a parked lightweight thread can be GC'ed + // ensure that a parked virtual thread can be GC'ed public void testGC2() { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread.start(); var ref = new WeakReference<>(thread); thread = null; waitUntilCleared(ref); } - // ensure that a terminated lightweight thread can be GC'ed + // ensure that a terminated virtual thread can be GC'ed public void testGC3() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { }); + var thread = Thread.newThread(Thread.VIRTUAL, () -> { }); thread.start(); thread.join(); var ref = new WeakReference<>(thread); diff --git a/test/jdk/java/lang/Thread/lightweight/Locking.java b/test/jdk/java/lang/Thread/virtual/Locking.java similarity index 81% rename from test/jdk/java/lang/Thread/lightweight/Locking.java rename to test/jdk/java/lang/Thread/virtual/Locking.java index 9f258d2d8fb..5cce998162d 100644 --- a/test/jdk/java/lang/Thread/lightweight/Locking.java +++ b/test/jdk/java/lang/Thread/virtual/Locking.java @@ -24,7 +24,7 @@ /** * @test * @run testng Locking - * @summary Test lightweight threads using java.util.concurrent locks + * @summary Test virtual threads using java.util.concurrent locks */ import java.util.concurrent.atomic.AtomicBoolean; @@ -39,7 +39,7 @@ public class Locking { // lock/unlock public void testReentrantLock1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { ReentrantLock lock = new ReentrantLock(); assertFalse(lock.isHeldByCurrentThread()); lock.lock(); @@ -51,7 +51,7 @@ public void testReentrantLock1() throws Exception { // tryLock/unlock public void testReentrantLock2() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { ReentrantLock lock = new ReentrantLock(); assertFalse(lock.isHeldByCurrentThread()); boolean acquired = lock.tryLock(); @@ -64,7 +64,7 @@ public void testReentrantLock2() throws Exception { // lock/lock/unlock/unlock public void testReentrantLock3() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { ReentrantLock lock = new ReentrantLock(); assertFalse(lock.isHeldByCurrentThread()); assertTrue(lock.getHoldCount() == 0); @@ -83,7 +83,7 @@ public void testReentrantLock3() throws Exception { }); } - // locked by dinoasur thread, lightweight thread tries to lock + // locked by dinoasur thread, virtual thread tries to lock public void testReentrantLock4() throws Exception { ReentrantLock lock = new ReentrantLock(); var holdsLock = new AtomicBoolean(); @@ -93,7 +93,7 @@ public void testReentrantLock4() throws Exception { // thread acquires lock lock.lock(); try { - thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + thread = Thread.newThread(Thread.VIRTUAL, () -> { lock.lock(); // should block holdsLock.set(true); LockSupport.park(); @@ -101,14 +101,14 @@ public void testReentrantLock4() throws Exception { holdsLock.set(false); }); thread.start(); - // give time for lightweight thread to block + // give time for virtual thread to block Thread.sleep(500); assertFalse(holdsLock.get()); } finally { lock.unlock(); } - // lightweight thread should acquire lock, park, unpark, and then release lock + // virtual thread should acquire lock, park, unpark, and then release lock while (!holdsLock.get()) { Thread.sleep(20); } @@ -119,10 +119,10 @@ public void testReentrantLock4() throws Exception { thread.join(); } - // locked by lightweight thread, dinoasur thread tries to lock + // locked by virtual thread, dinoasur thread tries to lock public void testReentrantLock5() throws Exception { ReentrantLock lock = new ReentrantLock(); - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { lock.lock(); try { LockSupport.park(); @@ -132,7 +132,7 @@ public void testReentrantLock5() throws Exception { }); thread.start(); - // wat for lightweight thread to acquire lock + // wat for virtual thread to acquire lock while (!lock.isLocked()) { Thread.sleep(20); } @@ -141,7 +141,7 @@ public void testReentrantLock5() throws Exception { try { assertFalse(lock.tryLock()); } finally { - // lightweight thread should unlock + // virtual thread should unlock LockSupport.unpark(thread); // thread should be able to acquire lock @@ -151,10 +151,10 @@ public void testReentrantLock5() throws Exception { } } - // lock by lightweight thread, another lightweight thread tries to lock + // lock by virtual thread, another virtual thread tries to lock public void testReentrantLock6() throws Exception { ReentrantLock lock = new ReentrantLock(); - var thread1 = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread1 = Thread.newThread(Thread.VIRTUAL, () -> { lock.lock(); try { LockSupport.park(); @@ -164,13 +164,13 @@ public void testReentrantLock6() throws Exception { }); thread1.start(); - // wat for lightweight thread to acquire lock + // wat for virtual thread to acquire lock while (!lock.isLocked()) { Thread.sleep(10); } var holdsLock = new AtomicBoolean(); - var thread2 = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread2 = Thread.newThread(Thread.VIRTUAL, () -> { lock.lock(); holdsLock.set(true); LockSupport.park(); @@ -179,18 +179,18 @@ public void testReentrantLock6() throws Exception { }); thread2.start(); - // lightweight thread2 should block + // virtual thread2 should block Thread.sleep(1000); assertFalse(holdsLock.get()); - // unpark lightweight thread1 + // unpark virtual thread1 LockSupport.unpark(thread1); - // lightweight thread2 should acquire lock + // virtual thread2 should acquire lock while (!holdsLock.get()) { Thread.sleep(20); } - // unpark lightweight thread and it should release lock + // unpark virtual thread and it should release lock LockSupport.unpark(thread2); while (holdsLock.get()) { Thread.sleep(20); diff --git a/test/jdk/java/lang/Thread/lightweight/NetSockets.java b/test/jdk/java/lang/Thread/virtual/NetSockets.java similarity index 91% rename from test/jdk/java/lang/Thread/lightweight/NetSockets.java rename to test/jdk/java/lang/Thread/virtual/NetSockets.java index 03833fe397d..47930a432c9 100644 --- a/test/jdk/java/lang/Thread/lightweight/NetSockets.java +++ b/test/jdk/java/lang/Thread/virtual/NetSockets.java @@ -24,7 +24,7 @@ /** * @test * @run testng NetSockets - * @summary Basic tests for lightweight threads using java.net.Socket/ServerSocket + * @summary Basic tests for virtual threads using java.net.Socket/ServerSocket */ import java.io.Closeable; @@ -50,7 +50,7 @@ public class NetSockets { * Socket read/write, no blocking. */ public void testSocketReadWrite1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { Socket s1 = connection.socket1(); Socket s2 = connection.socket2(); @@ -69,10 +69,10 @@ public void testSocketReadWrite1() throws Exception { } /** - * Lightweight thread blocks in read. + * Virtual thread blocks in read. */ public void testSocketReadWrite2() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { Socket s1 = connection.socket1(); Socket s2 = connection.socket2(); @@ -91,10 +91,10 @@ public void testSocketReadWrite2() throws Exception { } /** - * Lightweight thread blocks in write. + * Virtual thread blocks in write. */ public void testSocketReadWrite3() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { Socket s1 = connection.socket1(); Socket s2 = connection.socket2(); @@ -113,10 +113,10 @@ public void testSocketReadWrite3() throws Exception { } /** - * Lightweight thread blocks in read, peer closes connection. + * Virtual thread blocks in read, peer closes connection. */ public void testSocketReadPeerClose1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { Socket s1 = connection.socket1(); Socket s2 = connection.socket2(); @@ -130,10 +130,10 @@ public void testSocketReadPeerClose1() throws Exception { } /** - * Lightweight thread blocks in read, peer closes connection abruptly. + * Virtual thread blocks in read, peer closes connection abruptly. */ public void testSocketReadPeerClose2() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { Socket s1 = connection.socket1(); Socket s2 = connection.socket2(); @@ -152,10 +152,10 @@ public void testSocketReadPeerClose2() throws Exception { } /** - * Socket close while lightweight thread blocked in read. + * Socket close while virtual thread blocked in read. */ public void testSocketReadAsyncClose() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { Socket s = connection.socket1(); ScheduledCloser.schedule(s, DELAY); @@ -168,10 +168,10 @@ public void testSocketReadAsyncClose() throws Exception { } /** - * Socket close while lightweight thread blocked in write. + * Socket close while virtual thread blocked in write. */ public void testSocketWriteAsyncClose() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { Socket s = connection.socket1(); ScheduledCloser.schedule(s, DELAY); @@ -190,7 +190,7 @@ public void testSocketWriteAsyncClose() throws Exception { * ServerSocket accept, no blocking. */ public void testServerSocketAccept1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var listener = new ServerSocket(0)) { var socket1 = new Socket(listener.getInetAddress(), listener.getLocalPort()); // accept should not block @@ -202,10 +202,10 @@ public void testServerSocketAccept1() throws Exception { } /** - * Lightweight thread blocks in accept. + * Virtual thread blocks in accept. */ public void testServerSocketAccept2() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var listener = new ServerSocket(0)) { var socket1 = new Socket(); ScheduledConnector.schedule(socket1, listener.getLocalSocketAddress(), DELAY); @@ -218,10 +218,10 @@ public void testServerSocketAccept2() throws Exception { } /** - * ServerSocket close while lightweight thread blocked in accept. + * ServerSocket close while virtual thread blocked in accept. */ public void testServerSocketAcceptAsyncClose() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var listener = new ServerSocket(0)) { ScheduledCloser.schedule(listener, DELAY); try { diff --git a/test/jdk/java/lang/Thread/lightweight/NioChannels.java b/test/jdk/java/lang/Thread/virtual/NioChannels.java similarity index 88% rename from test/jdk/java/lang/Thread/lightweight/NioChannels.java rename to test/jdk/java/lang/Thread/virtual/NioChannels.java index ae6dd59da60..9b9f9bfc3b1 100644 --- a/test/jdk/java/lang/Thread/lightweight/NioChannels.java +++ b/test/jdk/java/lang/Thread/virtual/NioChannels.java @@ -24,7 +24,7 @@ /** * @test * @run testng NioChannels - * @summary Basic tests for lightweight threads doing blocking I/O with NIO channels + * @summary Basic tests for virtual threads doing blocking I/O with NIO channels */ import java.io.Closeable; @@ -56,7 +56,7 @@ public class NioChannels { * SocketChannel read/write, no blocking. */ public void testSocketChannelReadWrite1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { SocketChannel sc1 = connection.channel1(); SocketChannel sc2 = connection.channel2(); @@ -76,10 +76,10 @@ public void testSocketChannelReadWrite1() throws Exception { } /** - * Lightweight thread blocks in SocketChannel read. + * Virtual thread blocks in SocketChannel read. */ public void testSocketChannelRead() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { SocketChannel sc1 = connection.channel1(); SocketChannel sc2 = connection.channel2(); @@ -98,10 +98,10 @@ public void testSocketChannelRead() throws Exception { } /** - * Lightweight thread blocks in SocketChannel write. + * Virtual thread blocks in SocketChannel write. */ public void testSocketChannelWrite() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { SocketChannel sc1 = connection.channel1(); SocketChannel sc2 = connection.channel2(); @@ -121,10 +121,10 @@ public void testSocketChannelWrite() throws Exception { } /** - * SocketChannel close while lightweight thread blocked in read. + * SocketChannel close while virtual thread blocked in read. */ public void testSocketChannelReadAsyncClose() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { SocketChannel sc = connection.channel1(); ScheduledCloser.schedule(sc, DELAY); @@ -137,10 +137,10 @@ public void testSocketChannelReadAsyncClose() throws Exception { } /** - * Lightweight thread interrupted while blocked in SocketChannel read. + * Virtual thread interrupted while blocked in SocketChannel read. */ public void testSocketChannelReadInterrupt() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { SocketChannel sc = connection.channel1(); ScheduledInterrupter.schedule(Thread.currentThread(), DELAY); @@ -153,10 +153,10 @@ public void testSocketChannelReadInterrupt() throws Exception { } /** - * SocketChannel close while lightweight thread blocked in write. + * SocketChannel close while virtual thread blocked in write. */ public void testSocketChannelWriteAsyncClose() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { SocketChannel sc = connection.channel1(); ScheduledCloser.schedule(sc, DELAY); @@ -173,10 +173,10 @@ public void testSocketChannelWriteAsyncClose() throws Exception { } /** - * Lightweight thread interrupted while blocked in SocketChannel write. + * Virtual thread interrupted while blocked in SocketChannel write. */ public void testSocketChannelWriteInterrupt() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { SocketChannel sc = connection.channel1(); ScheduledInterrupter.schedule(Thread.currentThread(), DELAY); @@ -193,10 +193,10 @@ public void testSocketChannelWriteInterrupt() throws Exception { } /** - * Lightweight thread blocks in SocketChannel read. + * Virtual thread blocks in SocketChannel read. */ private void testSocketAdaptorRead(int timeout) throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var connection = new Connection()) { SocketChannel sc1 = connection.channel1(); SocketChannel sc2 = connection.channel2(); @@ -218,14 +218,14 @@ private void testSocketAdaptorRead(int timeout) throws Exception { } /** - * Lightweight thread blocks in SocketChannel adaptor read. + * Virtual thread blocks in SocketChannel adaptor read. */ public void testSocketAdaptorRead1() throws Exception { testSocketAdaptorRead(0); } /** - * Lightweight thread blocks in SocketChannel adaptor read with timeout. + * Virtual thread blocks in SocketChannel adaptor read with timeout. */ public void testSocketAdaptorRead2() throws Exception { testSocketAdaptorRead(60_000); @@ -235,7 +235,7 @@ public void testSocketAdaptorRead2() throws Exception { * ServerSocketChannel accept, no blocking. */ public void testServerSocketChannelAccept1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var ssc = ServerSocketChannel.open()) { ssc.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); var sc1 = SocketChannel.open(ssc.getLocalAddress()); @@ -248,10 +248,10 @@ public void testServerSocketChannelAccept1() throws Exception { } /** - * Lightweight thread blocks in ServerSocketChannel accept. + * Virtual thread blocks in ServerSocketChannel accept. */ public void testServerSocketChannelAccept2() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var ssc = ServerSocketChannel.open()) { ssc.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); var sc1 = SocketChannel.open(); @@ -265,10 +265,10 @@ public void testServerSocketChannelAccept2() throws Exception { } /** - * SeverSocketChannel close while lightweight thread blocked in accept. + * SeverSocketChannel close while virtual thread blocked in accept. */ public void testServerSocketChannelAcceptAsyncClose() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var ssc = ServerSocketChannel.open()) { InetAddress lh = InetAddress.getLocalHost(); ssc.bind(new InetSocketAddress(lh, 0)); @@ -283,10 +283,10 @@ public void testServerSocketChannelAcceptAsyncClose() throws Exception { } /** - * Lightweight thread interrupted while blocked in ServerSocketChannel accept. + * Virtual thread interrupted while blocked in ServerSocketChannel accept. */ public void testServerSocketChannelAcceptInterrupt() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var ssc = ServerSocketChannel.open()) { InetAddress lh = InetAddress.getLocalHost(); ssc.bind(new InetSocketAddress(lh, 0)); @@ -301,7 +301,7 @@ public void testServerSocketChannelAcceptInterrupt() throws Exception { } void testSocketChannelAdaptorAccept(int timeout) throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (var ssc = ServerSocketChannel.open()) { ssc.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); var sc1 = SocketChannel.open(); @@ -319,14 +319,14 @@ void testSocketChannelAdaptorAccept(int timeout) throws Exception { } /** - * Lightweight thread blocks in ServerSocketChannel adaptor accept. + * Virtual thread blocks in ServerSocketChannel adaptor accept. */ public void testSocketChannelAdaptorAccept1() throws Exception { testSocketChannelAdaptorAccept(0); } /** - * Lightweight thread blocks in ServerSocketChannel adaptor accept with timeout. + * Virtual thread blocks in ServerSocketChannel adaptor accept with timeout. */ public void testSocketChannelAdaptorAccept2() throws Exception { testSocketChannelAdaptorAccept(60_000); @@ -336,7 +336,7 @@ public void testSocketChannelAdaptorAccept2() throws Exception { * DatagramChannel receive/send, no blocking. */ public void testDatagramChannelSendReceive1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (DatagramChannel dc1 = DatagramChannel.open(); DatagramChannel dc2 = DatagramChannel.open()) { @@ -357,10 +357,10 @@ public void testDatagramChannelSendReceive1() throws Exception { } /** - * Lightweight thread blocks in DatagramChannel receive. + * Virtual thread blocks in DatagramChannel receive. */ public void testDatagramChannelSendReceive2() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (DatagramChannel dc1 = DatagramChannel.open(); DatagramChannel dc2 = DatagramChannel.open()) { @@ -380,10 +380,10 @@ public void testDatagramChannelSendReceive2() throws Exception { } /** - * DatagramChannel close while lightweight thread blocked in receive. + * DatagramChannel close while virtual thread blocked in receive. */ public void testDatagramChannelReceiveAsyncClose() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (DatagramChannel dc = DatagramChannel.open()) { InetAddress lh = InetAddress.getLocalHost(); dc.bind(new InetSocketAddress(lh, 0)); @@ -397,10 +397,10 @@ public void testDatagramChannelReceiveAsyncClose() throws Exception { } /** - * Lightweight thread interrupted while blocked in DatagramChannel receive. + * Virtual thread interrupted while blocked in DatagramChannel receive. */ public void testDatagramChannelReceiveInterrupt() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (DatagramChannel dc = DatagramChannel.open()) { InetAddress lh = InetAddress.getLocalHost(); dc.bind(new InetSocketAddress(lh, 0)); @@ -414,7 +414,7 @@ public void testDatagramChannelReceiveInterrupt() throws Exception { } void testDatagramSocketAdaptorReceive(int timeout) throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try (DatagramChannel dc1 = DatagramChannel.open(); DatagramChannel dc2 = DatagramChannel.open()) { @@ -437,14 +437,14 @@ void testDatagramSocketAdaptorReceive(int timeout) throws Exception { } /** - * Lightweight thread blocks in DatagramSocket adaptor receive + * Virtual thread blocks in DatagramSocket adaptor receive */ public void testDatagramSocketAdaptorReceive1() throws Exception { testDatagramSocketAdaptorReceive(0); } /** - * Lightweight thread blocks in DatagramSocket adaptor receive with timeout + * Virtual thread blocks in DatagramSocket adaptor receive with timeout */ public void testDatagramSocketAdaptorReceive2() throws Exception { testDatagramSocketAdaptorReceive(60_1000); @@ -454,7 +454,7 @@ public void testDatagramSocketAdaptorReceive2() throws Exception { * Pipe read/write, no blocking. */ public void testPipeReadWrite1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Pipe p = Pipe.open(); try (Pipe.SinkChannel sink = p.sink(); Pipe.SourceChannel source = p.source()) { @@ -474,10 +474,10 @@ public void testPipeReadWrite1() throws Exception { } /** - * Lightweight thread blocks in Pipe.SourceChannel read. + * Virtual thread blocks in Pipe.SourceChannel read. */ public void testPipeReadWrite2() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Pipe p = Pipe.open(); try (Pipe.SinkChannel sink = p.sink(); Pipe.SourceChannel source = p.source()) { @@ -496,10 +496,10 @@ public void testPipeReadWrite2() throws Exception { } /** - * Lightweight thread blocks in Pipe.SinkChannel write. + * Virtual thread blocks in Pipe.SinkChannel write. */ public void testPipeReadWrite3() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Pipe p = Pipe.open(); try (Pipe.SinkChannel sink = p.sink(); Pipe.SourceChannel source = p.source()) { @@ -519,10 +519,10 @@ public void testPipeReadWrite3() throws Exception { } /** - * Pipe.SourceChannel close while lightweight thread blocked in read. + * Pipe.SourceChannel close while virtual thread blocked in read. */ public void testPipeReadAsyncClose() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Pipe p = Pipe.open(); try (Pipe.SourceChannel source = p.source()) { ScheduledCloser.schedule(source, DELAY); @@ -535,10 +535,10 @@ public void testPipeReadAsyncClose() throws Exception { } /** - * Lightweight thread interrupted while blocked in Pipe.SourceChannel read. + * Virtual thread interrupted while blocked in Pipe.SourceChannel read. */ public void testPipeReadInterrupt() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Pipe p = Pipe.open(); try (Pipe.SourceChannel source = p.source()) { ScheduledInterrupter.schedule(Thread.currentThread(), DELAY); @@ -551,10 +551,10 @@ public void testPipeReadInterrupt() throws Exception { } /** - * Pipe.SinkChannel close while lightweight thread blocked in write. + * Pipe.SinkChannel close while virtual thread blocked in write. */ public void testPipeWriteAsyncClose() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Pipe p = Pipe.open(); try (Pipe.SinkChannel sink = p.sink()) { ScheduledCloser.schedule(sink, DELAY); @@ -571,10 +571,10 @@ public void testPipeWriteAsyncClose() throws Exception { } /** - * Lightweight thread interrupted while blocked in Pipe.SinkChannel write. + * Virtual thread interrupted while blocked in Pipe.SinkChannel write. */ public void testPipeWriteInterrupt() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Pipe p = Pipe.open(); try (Pipe.SinkChannel sink = p.sink()) { ScheduledInterrupter.schedule(Thread.currentThread(), DELAY); @@ -642,7 +642,7 @@ static void schedule(Closeable c, long delay) { } /** - * Interrupts a thread or lightweight thread after a delay + * Interrupts a thread or virtual thread after a delay */ static class ScheduledInterrupter implements Runnable { private final Thread thread; @@ -783,4 +783,4 @@ static void schedule(DatagramChannel dc, ByteBuffer buf, } } -} +} \ No newline at end of file diff --git a/test/jdk/java/lang/Thread/lightweight/Parking.java b/test/jdk/java/lang/Thread/virtual/Parking.java similarity index 72% rename from test/jdk/java/lang/Thread/lightweight/Parking.java rename to test/jdk/java/lang/Thread/virtual/Parking.java index cec27f21bd1..365ef69c87a 100644 --- a/test/jdk/java/lang/Thread/lightweight/Parking.java +++ b/test/jdk/java/lang/Thread/virtual/Parking.java @@ -24,7 +24,7 @@ /** * @test * @run testng Parking - * @summary Test lightweight threads using park/unpark + * @summary Test virtual threads using park/unpark */ import java.time.Duration; @@ -37,21 +37,21 @@ @Test public class Parking { - // lightweight thread parks, unparked by dinosaur thread + // virtual thread parks, unparked by dinosaur thread public void testPark1() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread.start(); - Thread.sleep(1000); // give time for lightweight thread to park + Thread.sleep(1000); // give time for virtual thread to park LockSupport.unpark(thread); thread.join(); } - // lightweight thread parks, unparked by another lightweight thread + // virtual thread parks, unparked by another virtual thread public void testPark2() throws Exception { - var thread1 = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread1 = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread1.start(); - Thread.sleep(1000); // give time for lightweight thread to park - var thread2 = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.unpark(thread1)); + Thread.sleep(1000); // give time for virtual thread to park + var thread2 = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.unpark(thread1)); thread2.start(); thread1.join(); thread2.join(); @@ -59,14 +59,14 @@ public void testPark2() throws Exception { // park while holding monitor public void testPark3() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { var lock = new Object(); synchronized (lock) { LockSupport.park(); } }); thread.start(); - Thread.sleep(1000); // give time for lightweight thread to park + Thread.sleep(1000); // give time for virtual thread to park LockSupport.unpark(thread); thread.join(); } @@ -75,7 +75,7 @@ public void testPark3() throws Exception { // park with native frame on the stack public void testPark4() throws Exception { - var lightweight thread = lightweight threadScope.background().schedule(() -> { + var virtual thread = virtual threadScope.background().schedule(() -> { try { Method m = Basic.class.getDeclaredMethod("doPark"); m.invoke(null); @@ -83,9 +83,9 @@ public void testPark4() throws Exception { assertTrue(false); } }); - Thread.sleep(1000); // give time for lightweight thread to park - LockSupport.unpark(lightweight thread); - lightweight thread.join(); + Thread.sleep(1000); // give time for virtual thread to park + LockSupport.unpark(virtual thread); + virtual thread.join(); } static void doPark() { LockSupport.park(); @@ -95,7 +95,7 @@ static void doPark() { // unpark before park public void testPark5() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { LockSupport.unpark(Thread.currentThread()); LockSupport.park(); }); @@ -105,7 +105,7 @@ public void testPark5() throws Exception { // 2 x unpark before park public void testPark6() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { Thread me = Thread.currentThread(); LockSupport.unpark(me); LockSupport.unpark(me); @@ -113,22 +113,22 @@ public void testPark6() throws Exception { LockSupport.park(); // should park }); thread.start(); - Thread.sleep(1000); // give time for lightweight thread to park + Thread.sleep(1000); // give time for virtual thread to park LockSupport.unpark(thread); thread.join(); } // 2 x park public void testPark7() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { LockSupport.park(); LockSupport.park(); }); thread.start(); - Thread.sleep(1000); // give time for lightweight thread to park + Thread.sleep(1000); // give time for virtual thread to park - // unpark, lightweight thread should park again + // unpark, virtual thread should park again LockSupport.unpark(thread); Thread.sleep(1000); assertTrue(thread.isAlive()); @@ -140,7 +140,7 @@ public void testPark7() throws Exception { // interrupt before park public void testPark8() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread t = Thread.currentThread(); t.interrupt(); LockSupport.park(); @@ -150,7 +150,7 @@ public void testPark8() throws Exception { // interrupt while parked public void testPark9() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread t = Thread.currentThread(); TestHelper.scheduleInterrupt(t, 1000); LockSupport.park(); @@ -160,7 +160,7 @@ public void testPark9() throws Exception { // interrupt before park (pinned park) public void testPark10() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread t = Thread.currentThread(); t.interrupt(); Object lock = new Object(); @@ -173,7 +173,7 @@ public void testPark10() throws Exception { // interrupt while parked (pinned park) public void testPark11() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread t = Thread.currentThread(); TestHelper.scheduleInterrupt(t, 1000); Object lock = new Object(); @@ -186,50 +186,50 @@ public void testPark11() throws Exception { // parkNanos(-1) completes immediately public void testParkNanos1() throws Exception { - TestHelper.runInLightweightThread(() -> LockSupport.parkNanos(-1)); + TestHelper.runInVirtualThread(() -> LockSupport.parkNanos(-1)); } // parkNanos(0) completes immediately public void testParkNanos2() throws Exception { - TestHelper.runInLightweightThread(() -> LockSupport.parkNanos(0)); + TestHelper.runInVirtualThread(() -> LockSupport.parkNanos(0)); } // parkNanos(1000ms) completes quickly public void testParkNanos3() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { // park for 1000ms long nanos = TimeUnit.NANOSECONDS.convert(1000, TimeUnit.MILLISECONDS); long start = System.nanoTime(); LockSupport.parkNanos(nanos); - // check that lightweight thread parks for >= 900ms + // check that virtual thread parks for >= 900ms long elapsed = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS); assertTrue(elapsed >= 900); }); } - // lightweight thread parks, unparked by dinosaur thread + // virtual thread parks, unparked by dinosaur thread public void testParkNanos4() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { long nanos = TimeUnit.NANOSECONDS.convert(30, TimeUnit.SECONDS); LockSupport.parkNanos(nanos); }); thread.start(); - Thread.sleep(1000); // give time for lightweight thread to park + Thread.sleep(1000); // give time for virtual thread to park LockSupport.unpark(thread); thread.join(); } - // lightweight thread parks, unparked by another lightweight thread + // virtual thread parks, unparked by another virtual thread public void testParkNanos5() throws Exception { - var thread1 = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread1 = Thread.newThread(Thread.VIRTUAL, () -> { long nanos = TimeUnit.NANOSECONDS.convert(30, TimeUnit.SECONDS); LockSupport.parkNanos(nanos); }); thread1.start(); - Thread.sleep(1000); // give time for lightweight thread to park - var thread2 = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.unpark(thread1)); + Thread.sleep(1000); // give time for virtual thread to park + var thread2 = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.unpark(thread1)); thread2.start(); thread1.join(); thread2.join(); @@ -237,7 +237,7 @@ public void testParkNanos5() throws Exception { // unpark before parkNanos public void testParkNanos6() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { LockSupport.unpark(Thread.currentThread()); long nanos = TimeUnit.NANOSECONDS.convert(30, TimeUnit.SECONDS); LockSupport.parkNanos(nanos); @@ -246,7 +246,7 @@ public void testParkNanos6() throws Exception { // unpark before parkNanos(0), should consume permit public void testParkNanos7() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { LockSupport.unpark(Thread.currentThread()); LockSupport.parkNanos(0); LockSupport.park(); // should block diff --git a/test/jdk/java/lang/Thread/lightweight/TestHelper.java b/test/jdk/java/lang/Thread/virtual/TestHelper.java similarity index 89% rename from test/jdk/java/lang/Thread/lightweight/TestHelper.java rename to test/jdk/java/lang/Thread/virtual/TestHelper.java index 4ddb1d4cea4..183653fbd40 100644 --- a/test/jdk/java/lang/Thread/lightweight/TestHelper.java +++ b/test/jdk/java/lang/Thread/virtual/TestHelper.java @@ -24,7 +24,7 @@ import java.util.concurrent.atomic.AtomicReference; /** - * Runs tasks in lightweight threads. + * Runs tasks in virtual threads. */ class TestHelper { @@ -36,7 +36,7 @@ interface ThrowingRunnable { private static void run(String name, int characteristics, ThrowingRunnable task) throws Exception { - characteristics |= Thread.LIGHTWEIGHT; + characteristics |= Thread.VIRTUAL; AtomicReference exc = new AtomicReference<>(); Runnable target = () -> { try { @@ -61,19 +61,19 @@ private static void run(String name, int characteristics, ThrowingRunnable task) } } - static void runInLightweightThread(String name, int characteristics, ThrowingRunnable task) + static void runInVirtualThread(String name, int characteristics, ThrowingRunnable task) throws Exception { run(name, characteristics, task); } - static void runInLightweightThread(int characteristics, ThrowingRunnable task) + static void runInVirtualThread(int characteristics, ThrowingRunnable task) throws Exception { run(null, characteristics, task); } - static void runInLightweightThread(ThrowingRunnable task) throws Exception { + static void runInVirtualThread(ThrowingRunnable task) throws Exception { run(null, 0, task); } diff --git a/test/jdk/java/lang/Thread/lightweight/ThreadAPI.java b/test/jdk/java/lang/Thread/virtual/ThreadAPI.java similarity index 83% rename from test/jdk/java/lang/Thread/lightweight/ThreadAPI.java rename to test/jdk/java/lang/Thread/virtual/ThreadAPI.java index 0cb09237e8e..0fbd24e7957 100644 --- a/test/jdk/java/lang/Thread/lightweight/ThreadAPI.java +++ b/test/jdk/java/lang/Thread/virtual/ThreadAPI.java @@ -24,7 +24,7 @@ /** * @test * @run testng ThreadAPI - * @summary Test Thread API with lightweight threads + * @summary Test Thread API with virtual threads */ import java.time.Duration; @@ -55,13 +55,13 @@ public class ThreadAPI { public void testCurrentThread1() throws Exception { var before = new AtomicReference(); var after = new AtomicReference(); - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { before.set(Thread.currentThread()); LockSupport.park(); after.set(Thread.currentThread()); }); thread.start(); - Thread.sleep(100); // give time for lightweight thread to park + Thread.sleep(100); // give time for virtual thread to park LockSupport.unpark(thread); thread.join(); assertTrue(before.get() == thread); @@ -74,7 +74,7 @@ public void testCurrentThread2() throws Exception { var ref2 = new AtomicReference(); var ref3 = new AtomicReference(); var lock = new Object(); - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { ref1.set(Thread.currentThread()); synchronized (lock) { ref2.set(Thread.currentThread()); @@ -83,7 +83,7 @@ public void testCurrentThread2() throws Exception { }); synchronized (lock) { thread.start(); - Thread.sleep(100); // give time for lightweight thread to block + Thread.sleep(100); // give time for virtual thread to block } thread.join(); assertTrue(ref1.get() == thread); @@ -97,7 +97,7 @@ public void testCurrentThread3() throws Exception { var ref2 = new AtomicReference(); var ref3 = new AtomicReference(); var lock = new ReentrantLock(); - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { ref1.set(Thread.currentThread()); lock.lock(); try { @@ -110,7 +110,7 @@ public void testCurrentThread3() throws Exception { lock.lock(); try { thread.start(); - Thread.sleep(100); // give time for lightweight thread to block + Thread.sleep(100); // give time for virtual thread to block } finally { lock.unlock(); } @@ -125,7 +125,7 @@ public void testCurrentThread3() throws Exception { public void testRun1() throws Exception { var ref = new AtomicBoolean(); - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> ref.set(true)); + var thread = Thread.newThread(Thread.VIRTUAL, () -> ref.set(true)); thread.run(); assertFalse(ref.get()); } @@ -135,7 +135,7 @@ public void testRun1() throws Exception { // already started public void testStart1() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread.start(); try { assertThrows(IllegalThreadStateException.class, thread::start); @@ -147,7 +147,7 @@ public void testStart1() throws Exception { // already terminated public void testStart2() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { }); + var thread = Thread.newThread(Thread.VIRTUAL, () -> { }); thread.start(); thread.join(); assertThrows(IllegalThreadStateException.class, thread::start); @@ -157,7 +157,7 @@ public void testStart2() throws Exception { // -- stop/suspend/resume -- public void testStop1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread t = Thread.currentThread(); try { t.stop(); @@ -169,7 +169,7 @@ public void testStop1() throws Exception { } public void testStop2() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { try { Thread.sleep(20*1000); } catch (InterruptedException e) { } @@ -184,7 +184,7 @@ public void testStop2() throws Exception { } public void testSuspend1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread t = Thread.currentThread(); try { t.suspend(); @@ -196,7 +196,7 @@ public void testSuspend1() throws Exception { } public void testSuspend2() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { try { Thread.sleep(20*1000); } catch (InterruptedException e) { } @@ -211,7 +211,7 @@ public void testSuspend2() throws Exception { } public void testResume1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread t = Thread.currentThread(); try { t.resume(); @@ -223,7 +223,7 @@ public void testResume1() throws Exception { } public void testResume2() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { try { Thread.sleep(20*1000); } catch (InterruptedException e) { } @@ -242,7 +242,7 @@ public void testResume2() throws Exception { // join before start public void testJoin1() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { }); + var thread = Thread.newThread(Thread.VIRTUAL, () -> { }); // invoke join from dinosaur thread thread.join(); @@ -258,12 +258,12 @@ public void testJoin1() throws Exception { // join before start public void testJoin2() throws Exception { - TestHelper.runInLightweightThread(this::testJoin1); + TestHelper.runInVirtualThread(this::testJoin1); } // join where thread does not terminate public void testJoin3() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread.start(); try { thread.join(100); @@ -279,12 +279,12 @@ public void testJoin3() throws Exception { // join where thread does not terminate public void testJoin4() throws Exception { - TestHelper.runInLightweightThread(this::testJoin3); + TestHelper.runInVirtualThread(this::testJoin3); } // join where thread terminates public void testJoin5() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { try { Thread.sleep(100); } catch (InterruptedException e) { } @@ -296,12 +296,12 @@ public void testJoin5() throws Exception { // join where thread terminates public void testJoin6() throws Exception { - TestHelper.runInLightweightThread(this::testJoin5); + TestHelper.runInVirtualThread(this::testJoin5); } // join where thread terminates public void testJoin7() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { try { Thread.sleep(100); } catch (InterruptedException e) { } @@ -313,12 +313,12 @@ public void testJoin7() throws Exception { // join where thread terminates public void testJoin8() throws Exception { - TestHelper.runInLightweightThread(this::testJoin7); + TestHelper.runInVirtualThread(this::testJoin7); } // join where thread terminates public void testJoin9() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { try { Thread.sleep(100); } catch (InterruptedException e) { } @@ -330,12 +330,12 @@ public void testJoin9() throws Exception { // join where thread terminates public void testJoin10() throws Exception { - TestHelper.runInLightweightThread(this::testJoin9); + TestHelper.runInVirtualThread(this::testJoin9); } // join where thread terminates public void testJoin11() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { try { Thread.sleep(100); } catch (InterruptedException e) { } @@ -347,12 +347,12 @@ public void testJoin11() throws Exception { // join where thread terminates public void testJoin12() throws Exception { - TestHelper.runInLightweightThread(this::testJoin11); + TestHelper.runInVirtualThread(this::testJoin11); } // join thread that has terminated public void testJoin13() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { }); + var thread = Thread.newThread(Thread.VIRTUAL, () -> { }); thread.start(); while (thread.isAlive()) { Thread.sleep(10); @@ -366,12 +366,12 @@ public void testJoin13() throws Exception { // join where thread terminates public void testJoin14() throws Exception { - TestHelper.runInLightweightThread(this::testJoin13); + TestHelper.runInVirtualThread(this::testJoin13); } // interrupt status before join public void testJoin15() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread.start(); Thread.currentThread().interrupt(); try { @@ -388,12 +388,12 @@ public void testJoin15() throws Exception { // interrupt status before join public void testJoin16() throws Exception { - TestHelper.runInLightweightThread(this::testJoin15); + TestHelper.runInVirtualThread(this::testJoin15); } // interrupt status before join public void testJoin17() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread.start(); Thread.currentThread().interrupt(); try { @@ -410,12 +410,12 @@ public void testJoin17() throws Exception { // interrupt status before join public void testJoin18() throws Exception { - TestHelper.runInLightweightThread(this::testJoin17); + TestHelper.runInVirtualThread(this::testJoin17); } // interrupt status before join public void testJoin19() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread.start(); Thread.currentThread().interrupt(); try { @@ -432,12 +432,12 @@ public void testJoin19() throws Exception { // interrupt status before join public void testJoin20() throws Exception { - TestHelper.runInLightweightThread(this::testJoin19); + TestHelper.runInVirtualThread(this::testJoin19); } // interrupted when in join public void testJoin21() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread.start(); TestHelper.scheduleInterrupt(Thread.currentThread(), 100); try { @@ -454,12 +454,12 @@ public void testJoin21() throws Exception { // interrupted when in join public void testJoin22() throws Exception { - TestHelper.runInLightweightThread(this::testJoin17); + TestHelper.runInVirtualThread(this::testJoin17); } // interrupted when in join public void testJoin23() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread.start(); TestHelper.scheduleInterrupt(Thread.currentThread(), 100); try { @@ -476,12 +476,12 @@ public void testJoin23() throws Exception { // interrupted when in join public void testJoin24() throws Exception { - TestHelper.runInLightweightThread(this::testJoin23); + TestHelper.runInVirtualThread(this::testJoin23); } // interrupted when in join public void testJoin25() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread.start(); TestHelper.scheduleInterrupt(Thread.currentThread(), 100); try { @@ -498,12 +498,12 @@ public void testJoin25() throws Exception { // interrupted when in join public void testJoin26() throws Exception { - TestHelper.runInLightweightThread(this::testJoin25); + TestHelper.runInVirtualThread(this::testJoin25); } - // join dinosaur thread from lightweight thread + // join dinosaur thread from virtual thread public void testJoin27() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { var thread = new Thread(() -> LockSupport.park()); thread.start(); try { @@ -517,10 +517,10 @@ public void testJoin27() throws Exception { }); } - // join dinosaur thread from lightweight thread + // join dinosaur thread from virtual thread public void testJoin28() throws Exception { long nanos = TimeUnit.NANOSECONDS.convert(2, TimeUnit.SECONDS); - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { var thread = new Thread(() -> LockSupport.parkNanos(nanos)); thread.start(); try { @@ -533,9 +533,9 @@ public void testJoin28() throws Exception { }); } - // join dinosaur thread from lightweight thread with interrupt status set + // join dinosaur thread from virtual thread with interrupt status set public void testJoin29() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { var thread = new Thread(() -> LockSupport.park()); thread.start(); Thread.currentThread().interrupt(); @@ -551,9 +551,9 @@ public void testJoin29() throws Exception { }); } - // interrupt lightweight thread when in join of dinosaur thread + // interrupt virtual thread when in join of dinosaur thread public void testJoin30() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { var thread = new Thread(() -> LockSupport.park()); thread.start(); TestHelper.scheduleInterrupt(Thread.currentThread(), 100); @@ -573,7 +573,7 @@ public void testJoin30() throws Exception { // -- interrupt -- public void testInterrupt1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); assertFalse(me.isInterrupted()); me.interrupt(); @@ -586,14 +586,14 @@ public void testInterrupt1() throws Exception { // interrupt before started public void testInterrupt2() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { }); + var thread = Thread.newThread(Thread.VIRTUAL, () -> { }); thread.interrupt(); assertTrue(thread.isInterrupted()); } // interrupt after terminated public void testInterrupt3() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { }); + var thread = Thread.newThread(Thread.VIRTUAL, () -> { }); thread.start(); thread.join(); thread.interrupt(); @@ -602,7 +602,7 @@ public void testInterrupt3() throws Exception { // terminate with interrupt status set public void testInterrupt4() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { Thread.currentThread().interrupt(); }); thread.start(); @@ -613,7 +613,7 @@ public void testInterrupt4() throws Exception { // interrupt when mounted public void testInterrupt5() throws Exception { var exception = new AtomicReference(); - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { try { try (var sel = Selector.open()) { sel.select(); @@ -633,7 +633,7 @@ public void testInterrupt5() throws Exception { // interrupt when mounted public void testInterrupt6() throws Exception { var exception = new AtomicReference(); - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { try { try { Thread.sleep(60*1000); @@ -656,7 +656,7 @@ public void testInterrupt6() throws Exception { // interrupt when unmounted public void testInterrupt7() throws Exception { var exception = new AtomicReference(); - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { try { LockSupport.park(); assertTrue(Thread.currentThread().isInterrupted()); @@ -673,14 +673,14 @@ public void testInterrupt7() throws Exception { // try to block with interrupt status set public void testInterrupt8() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); me.interrupt(); LockSupport.park(); assertTrue(Thread.interrupted()); }); - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); me.interrupt(); Object lock = new Object(); @@ -694,7 +694,7 @@ public void testInterrupt8() throws Exception { } }); - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); me.interrupt(); try (Selector sel = Selector.open()) { @@ -710,7 +710,7 @@ public void testInterrupt8() throws Exception { // create without a name public void testSetName1() throws Exception { // initially unnamed - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); assertEquals(me.getName(), ""); me.setName("fred"); @@ -720,7 +720,7 @@ public void testSetName1() throws Exception { // create without a name public void testSetName2() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { LockSupport.park(); }); thread.start(); @@ -736,7 +736,7 @@ public void testSetName2() throws Exception { // create with a name public void testSetName3() throws Exception { - TestHelper.runInLightweightThread("fred", 0, () -> { + TestHelper.runInVirtualThread("fred", 0, () -> { Thread me = Thread.currentThread(); assertEquals(me.getName(), "fred"); me.setName("joe"); @@ -764,7 +764,7 @@ public void testSetName4() throws Exception { // -- setPriority/getPriority -- public void testSetPriority1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); assertTrue(me.getPriority() == Thread.NORM_PRIORITY); me.setPriority(Thread.MIN_PRIORITY); @@ -773,7 +773,7 @@ public void testSetPriority1() throws Exception { } public void testSetPriority2() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { LockSupport.park(); }); thread.start(); @@ -791,21 +791,21 @@ public void testSetPriority2() throws Exception { // -- setDaemon/isDaemon -- public void testIsDaemon1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); assertTrue(me.isDaemon()); }); } public void testSetDaemon1() { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { }); + var thread = Thread.newThread(Thread.VIRTUAL, () -> { }); assertTrue(thread.isDaemon()); thread.setDaemon(false); assertTrue(thread.isDaemon()); } public void testSetDaemon2() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread.start(); try { assertTrue(thread.isDaemon()); @@ -823,8 +823,8 @@ public void testYield1() throws Exception { var list = new CopyOnWriteArrayList(); ExecutorService pool = Executors.newFixedThreadPool(1); ThreadFactory factory = Thread.builder() - .lightweight() - .executor(pool) + .virtual() + .scheduler(pool) .factory(); try { var thread = factory.newThread(() -> { @@ -851,8 +851,8 @@ public void testYield2() throws Exception { var list = new CopyOnWriteArrayList(); ExecutorService pool = Executors.newFixedThreadPool(1); ThreadFactory factory = Thread.builder() - .lightweight() - .executor(pool) + .virtual() + .scheduler(pool) .factory(); try { var thread = factory.newThread(() -> { @@ -880,7 +880,7 @@ public void testYield2() throws Exception { // -- Thread.onSpinWait -- public void testOnSpinWait() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); Thread.onSpinWait(); assertTrue(Thread.currentThread() == me); @@ -892,7 +892,7 @@ public void testOnSpinWait() throws Exception { // Thread.sleep(-1) public void testSleep1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { try { Thread.sleep(-1); assertTrue(false); @@ -900,24 +900,24 @@ public void testSleep1() throws Exception { // expected } }); - TestHelper.runInLightweightThread(() -> Thread.sleep(Duration.ofMillis(-1))); + TestHelper.runInVirtualThread(() -> Thread.sleep(Duration.ofMillis(-1))); } // Thread.sleep(0) public void testSleep2() throws Exception { - TestHelper.runInLightweightThread(() -> Thread.sleep(0)); - TestHelper.runInLightweightThread(() -> Thread.sleep(Duration.ofMillis(0))); + TestHelper.runInVirtualThread(() -> Thread.sleep(0)); + TestHelper.runInVirtualThread(() -> Thread.sleep(Duration.ofMillis(0))); } // Thread.sleep(2000) public void testSleep3() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { long start = System.currentTimeMillis(); Thread.sleep(2000); long elapsed = System.currentTimeMillis() - start; assertTrue(elapsed > 1900); }); - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { long start = System.currentTimeMillis(); Thread.sleep(Duration.ofMillis(2000)); long elapsed = System.currentTimeMillis() - start; @@ -927,7 +927,7 @@ public void testSleep3() throws Exception { // Thread.sleep with interrupt status set public void testSleep4() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); me.interrupt(); try { @@ -939,7 +939,7 @@ public void testSleep4() throws Exception { } }); - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); me.interrupt(); try { @@ -951,14 +951,14 @@ public void testSleep4() throws Exception { } }); - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); me.interrupt(); Thread.sleep(Duration.ofMillis(-1000)); // does nothing assertTrue(me.isInterrupted()); }); - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); me.interrupt(); try { @@ -970,7 +970,7 @@ public void testSleep4() throws Exception { } }); - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); me.interrupt(); try { @@ -985,7 +985,7 @@ public void testSleep4() throws Exception { // Thread.sleep interrupted while sleeping public void testSleep5() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread t = Thread.currentThread(); TestHelper.scheduleInterrupt(t, 2000); try { @@ -997,7 +997,7 @@ public void testSleep5() throws Exception { } }); - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread t = Thread.currentThread(); TestHelper.scheduleInterrupt(t, 2000); try { @@ -1010,10 +1010,10 @@ public void testSleep5() throws Exception { }); } - // Thread.sleep should not be disrupted by unparking lightweight thread + // Thread.sleep should not be disrupted by unparking virtual thread public void testSleep6() throws Exception { AtomicReference exc = new AtomicReference<>(); - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { long start = System.currentTimeMillis(); try { Thread.sleep(2000); @@ -1044,7 +1044,7 @@ public void testSleep6() throws Exception { public void testContextClassLoader1() throws Exception { ClassLoader loader = new ClassLoader() { }; - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread t = Thread.currentThread(); t.setContextClassLoader(loader); assertTrue(t.getContextClassLoader() == loader); @@ -1058,7 +1058,7 @@ public void testContextClassLoader2() throws Exception { ClassLoader savedLoader = t.getContextClassLoader(); t.setContextClassLoader(loader); try { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { assertTrue(Thread.currentThread().getContextClassLoader() == loader); }); } finally { @@ -1066,26 +1066,26 @@ public void testContextClassLoader2() throws Exception { } } - // inherit context class loader from creating lightweight thread + // inherit context class loader from creating virtual thread public void testContextClassLoader3() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { ClassLoader loader = new ClassLoader() { }; Thread.currentThread().setContextClassLoader(loader); - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { assertTrue(Thread.currentThread().getContextClassLoader() == loader); }); }); } - // inherit context class loader from creating lightweight thread + // inherit context class loader from creating virtual thread public void testContextClassLoader4() throws Exception { ClassLoader loader = new ClassLoader() { }; Thread t = Thread.currentThread(); ClassLoader savedLoader = t.getContextClassLoader(); t.setContextClassLoader(loader); try { - TestHelper.runInLightweightThread(() -> { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { + TestHelper.runInVirtualThread(() -> { assertTrue(Thread.currentThread().getContextClassLoader() == loader); }); }); @@ -1102,7 +1102,7 @@ public void testUncaughtExceptionHandler1() throws Exception { class FooException extends RuntimeException { } var exception = new AtomicReference(); Thread.UncaughtExceptionHandler handler = (thread, exc) -> exception.set(exc); - Thread thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + Thread thread = Thread.newThread(Thread.VIRTUAL, () -> { Thread me = Thread.currentThread(); assertTrue(me.getUncaughtExceptionHandler() == me.getThreadGroup()); me.setUncaughtExceptionHandler(handler); @@ -1122,7 +1122,7 @@ class FooException extends RuntimeException { } Thread.UncaughtExceptionHandler savedHandler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(handler); try { - Thread thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + Thread thread = Thread.newThread(Thread.VIRTUAL, () -> { Thread me = Thread.currentThread(); throw new FooException(); }); @@ -1140,8 +1140,8 @@ class FooException extends RuntimeException { } public void testGetId() throws Exception { var ref1 = new AtomicReference(); var ref2 = new AtomicReference(); - TestHelper.runInLightweightThread(() -> ref1.set(Thread.currentThread().getId())); - TestHelper.runInLightweightThread(() -> ref2.set(Thread.currentThread().getId())); + TestHelper.runInVirtualThread(() -> ref1.set(Thread.currentThread().getId())); + TestHelper.runInVirtualThread(() -> ref2.set(Thread.currentThread().getId())); long id1 = ref1.get(); long id2 = ref2.get(); long id3 = Thread.currentThread().getId(); @@ -1155,13 +1155,13 @@ public void testGetId() throws Exception { // NEW public void testGetState1() { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { }); + var thread = Thread.newThread(Thread.VIRTUAL, () -> { }); assertTrue(thread.getState() == Thread.State.NEW); } // RUNNABLE public void testGetState2() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread.State state = Thread.currentThread().getState(); assertTrue(state == Thread.State.RUNNABLE); }); @@ -1169,7 +1169,7 @@ public void testGetState2() throws Exception { // WAITING when parked public void testGetState3() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread.start(); while (thread.getState() != Thread.State.WAITING) { Thread.sleep(20); @@ -1180,7 +1180,7 @@ public void testGetState3() throws Exception { // WAITING when parked and pinned public void testGetState4() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { var lock = new Object(); synchronized (lock) { LockSupport.park(); @@ -1196,7 +1196,7 @@ public void testGetState4() throws Exception { // WAITING when blocked in Object.wait public void testGetState5() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { var lock = new Object(); synchronized (lock) { try { lock.wait(); } catch (InterruptedException e) { } @@ -1212,7 +1212,7 @@ public void testGetState5() throws Exception { // TERMINATED public void testGetState6() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { }); + var thread = Thread.newThread(Thread.VIRTUAL, () -> { }); thread.start(); thread.join(); assertTrue(thread.getState() == Thread.State.TERMINATED); @@ -1222,7 +1222,7 @@ public void testGetState6() throws Exception { // -- isAlive -- public void testIsAlive1() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); assertFalse(thread.isAlive()); thread.start(); try { @@ -1238,14 +1238,14 @@ public void testIsAlive1() throws Exception { // -- Thread.holdsLock -- public void testHoldsLock1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { var lock = new Object(); assertFalse(Thread.holdsLock(lock)); }); } public void testHoldsLock2() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { var lock = new Object(); synchronized (lock) { assertTrue(Thread.holdsLock(lock)); @@ -1259,7 +1259,7 @@ public void testHoldsLock2() throws Exception { // runnable (mounted) public void testGetStackTrace1() throws Exception { var sel = Selector.open(); - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { try { sel.select(); } catch (Exception e) { } }); thread.start(); @@ -1277,7 +1277,7 @@ public void testGetStackTrace1() throws Exception { // waiting (mounted) public void testGetStackTrace2() throws Exception { var lock = new Object(); - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { synchronized (lock) { try { lock.wait(); } catch (InterruptedException e) { } } @@ -1301,7 +1301,7 @@ public void testGetStackTrace2() throws Exception { // parked (unmounted) public void testGetStackTrace3() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); thread.start(); // wait for thread to park @@ -1320,14 +1320,14 @@ public void testGetStackTrace3() throws Exception { // not started public void testGetStackTrace4() { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { }); + var thread = Thread.newThread(Thread.VIRTUAL, () -> { }); StackTraceElement[] stack = thread.getStackTrace(); assertTrue(stack.length == 0); } // terminated public void testGetStackTrace5() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { }); + var thread = Thread.newThread(Thread.VIRTUAL, () -> { }); thread.start(); thread.join(); StackTraceElement[] stack = thread.getStackTrace(); @@ -1344,9 +1344,9 @@ private boolean contains(StackTraceElement[] stack, String expected) { // -- Thread.getAllStackTraces -- public void testGetAllStackTraces() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Set threads = Thread.getAllStackTraces().keySet(); - assertFalse(threads.stream().anyMatch(Thread::isLightweight)); + assertFalse(threads.stream().anyMatch(Thread::isVirtual)); }); } @@ -1354,7 +1354,7 @@ public void testGetAllStackTraces() throws Exception { // -- ThreadGroup -- public void testThreadGroup1() throws Exception { - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> LockSupport.park()); + var thread = Thread.newThread(Thread.VIRTUAL, () -> LockSupport.park()); var group = thread.getThreadGroup(); assertTrue(group != null); thread.start(); @@ -1375,17 +1375,17 @@ public void testThreadGroup1() throws Exception { } public void testActiveCount1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { assertTrue(Thread.activeCount() == 0); }); } - // Thread.enumerate should not enumerate lightweight threads + // Thread.enumerate should not enumerate virtual threads public void testEnumerate1() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread[] threads = new Thread[100]; int n = Thread.enumerate(threads); - assertFalse(Arrays.stream(threads, 0, n).anyMatch(Thread::isLightweight)); + assertFalse(Arrays.stream(threads, 0, n).anyMatch(Thread::isVirtual)); }); } @@ -1394,14 +1394,14 @@ public void testEnumerate1() throws Exception { // not started public void testToString1() { - Thread thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { }); + Thread thread = Thread.newThread(Thread.VIRTUAL, () -> { }); thread.setName("fred"); assertTrue(thread.toString().contains("fred")); } // mounted public void testToString2() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread me = Thread.currentThread(); me.setName("fred"); assertTrue(me.toString().contains("fred")); @@ -1410,7 +1410,7 @@ public void testToString2() throws Exception { // unmounted public void testToString3() throws Exception { - Thread thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + Thread thread = Thread.newThread(Thread.VIRTUAL, () -> { Thread me = Thread.currentThread(); me.setName("fred"); LockSupport.park(); @@ -1429,7 +1429,7 @@ public void testToString3() throws Exception { // terminated public void testToString4() throws Exception { - Thread thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + Thread thread = Thread.newThread(Thread.VIRTUAL, () -> { Thread me = Thread.currentThread(); me.setName("fred"); }); diff --git a/test/jdk/java/lang/Thread/lightweight/ThreadLocals.java b/test/jdk/java/lang/Thread/virtual/ThreadLocals.java similarity index 85% rename from test/jdk/java/lang/Thread/lightweight/ThreadLocals.java rename to test/jdk/java/lang/Thread/virtual/ThreadLocals.java index 61c297ae876..637477a8265 100644 --- a/test/jdk/java/lang/Thread/lightweight/ThreadLocals.java +++ b/test/jdk/java/lang/Thread/virtual/ThreadLocals.java @@ -24,7 +24,7 @@ /** * @test * @run testng ThreadLocals - * @summary Test Lightweight threads using thread locals + * @summary Test Virtual threads using thread locals */ import org.testng.annotations.Test; @@ -37,7 +37,7 @@ public class ThreadLocals { public void testThreadLocal1() throws Exception { for (int i = 0; i < 10; i++) { - TestHelper.runInLightweightThread(0, () -> { + TestHelper.runInVirtualThread(0, () -> { assertTrue(LOCAL.get() == null); Object obj = new Object(); LOCAL.set(obj); @@ -47,7 +47,7 @@ public void testThreadLocal1() throws Exception { } public void testThreadLocal2() throws Exception { - TestHelper.runInLightweightThread(0, () -> { + TestHelper.runInVirtualThread(0, () -> { assertTrue(LOCAL.get() == null); Object obj = new Object(); LOCAL.set(obj); @@ -58,7 +58,7 @@ public void testThreadLocal2() throws Exception { // no thread locals public void testThreadLocal3() throws Exception { - TestHelper.runInLightweightThread(Thread.NO_THREAD_LOCALS, () -> { + TestHelper.runInVirtualThread(Thread.NO_THREAD_LOCALS, () -> { assertThrows(UnsupportedOperationException.class, () -> LOCAL.set(null)); assertThrows(UnsupportedOperationException.class, () -> LOCAL.set(new Object())); assertThrows(UnsupportedOperationException.class, LOCAL::get); @@ -74,7 +74,7 @@ public void testThreadLocal3() throws Exception { public void testInheritedThreadLocal1() throws Exception { assertTrue(INHERITED_LOCAL.get() == null); for (int i = 0; i < 10; i++) { - TestHelper.runInLightweightThread(Thread.INHERIT_THREAD_LOCALS, () -> { + TestHelper.runInVirtualThread(Thread.INHERIT_THREAD_LOCALS, () -> { assertTrue(INHERITED_LOCAL.get() == null); Object obj = new Object(); INHERITED_LOCAL.set(obj); @@ -90,7 +90,7 @@ public void testInheritedThreadLocal2() throws Exception { var obj = new Object(); INHERITED_LOCAL.set(obj); try { - TestHelper.runInLightweightThread(Thread.INHERIT_THREAD_LOCALS, () -> { + TestHelper.runInVirtualThread(Thread.INHERIT_THREAD_LOCALS, () -> { assertTrue(INHERITED_LOCAL.get() == obj); }); } finally { @@ -98,13 +98,13 @@ public void testInheritedThreadLocal2() throws Exception { } } - // inherit thread local from Lightweight thread + // inherit thread local from Virtual thread public void testInheritedThreadLocal3() throws Exception { assertTrue(INHERITED_LOCAL.get() == null); - TestHelper.runInLightweightThread(0, () -> { + TestHelper.runInVirtualThread(0, () -> { var obj = new Object(); INHERITED_LOCAL.set(obj); - TestHelper.runInLightweightThread(Thread.INHERIT_THREAD_LOCALS, () -> { + TestHelper.runInVirtualThread(Thread.INHERIT_THREAD_LOCALS, () -> { assertTrue(INHERITED_LOCAL.get() == obj); }); assertTrue(INHERITED_LOCAL.get() == obj); @@ -119,7 +119,7 @@ public void testInheritedThreadLocal4() throws Exception { var obj = new Object(); INHERITED_LOCAL.set(obj); try { - TestHelper.runInLightweightThread(0, () -> { + TestHelper.runInVirtualThread(0, () -> { assertTrue(INHERITED_LOCAL.get() == null); }); } finally { @@ -127,13 +127,13 @@ public void testInheritedThreadLocal4() throws Exception { } } - // thread local not inherited from Lightweight thread + // thread local not inherited from Virtual thread public void testInheritedThreadLocal5() throws Exception { assertTrue(INHERITED_LOCAL.get() == null); - TestHelper.runInLightweightThread(0, () -> { + TestHelper.runInVirtualThread(0, () -> { var obj = new Object(); INHERITED_LOCAL.set(obj); - TestHelper.runInLightweightThread(0, () -> { + TestHelper.runInVirtualThread(0, () -> { assertTrue(INHERITED_LOCAL.get() == null); }); assertTrue(INHERITED_LOCAL.get() == obj); diff --git a/test/jdk/java/lang/Thread/lightweight/WaitNotify.java b/test/jdk/java/lang/Thread/virtual/WaitNotify.java similarity index 85% rename from test/jdk/java/lang/Thread/lightweight/WaitNotify.java rename to test/jdk/java/lang/Thread/virtual/WaitNotify.java index 607ca0f399a..979cd9d4ee6 100644 --- a/test/jdk/java/lang/Thread/lightweight/WaitNotify.java +++ b/test/jdk/java/lang/Thread/virtual/WaitNotify.java @@ -24,7 +24,7 @@ /** * @test * @run testng WaitNotify - * @summary Test lightweight threads using Object.wait/notify + * @summary Test virtual threads using Object.wait/notify */ import java.util.concurrent.Semaphore; @@ -35,11 +35,11 @@ @Test public class WaitNotify { - // lightweight thread waits, notified by dinosaur thread + // virtual thread waits, notified by dinosaur thread public void testWaitNotify1() throws Exception { var lock = new Object(); var ready = new Semaphore(0); - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { synchronized (lock) { ready.release(); try { @@ -56,11 +56,11 @@ public void testWaitNotify1() throws Exception { thread.join(); } - // dinosaur thread waits, notified by lightweight thread + // dinosaur thread waits, notified by virtual thread public void testWaitNotify2() throws Exception { var lock = new Object(); var ready = new Semaphore(0); - var thread = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread = Thread.newThread(Thread.VIRTUAL, () -> { ready.acquireUninterruptibly(); synchronized (lock) { lock.notifyAll(); @@ -74,11 +74,11 @@ public void testWaitNotify2() throws Exception { thread.join(); } - // lightweight thread waits, notified by other lightweight thread + // virtual thread waits, notified by other virtual thread public void testWaitNotify3() throws Exception { var lock = new Object(); var ready = new Semaphore(0); - var thread1 = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread1 = Thread.newThread(Thread.VIRTUAL, () -> { synchronized (lock) { ready.release(); try { @@ -86,7 +86,7 @@ public void testWaitNotify3() throws Exception { } catch (InterruptedException e) { } } }); - var thread2 = Thread.newThread(Thread.LIGHTWEIGHT, () -> { + var thread2 = Thread.newThread(Thread.VIRTUAL, () -> { ready.acquireUninterruptibly(); synchronized (lock) { lock.notifyAll(); @@ -98,7 +98,7 @@ public void testWaitNotify3() throws Exception { // interrupt before Object.wait public void testWaitNotify4() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread t = Thread.currentThread(); t.interrupt(); Object lock = new Object(); @@ -116,7 +116,7 @@ public void testWaitNotify4() throws Exception { // interrupt while waiting in Object.wait public void testWaitNotify5() throws Exception { - TestHelper.runInLightweightThread(() -> { + TestHelper.runInVirtualThread(() -> { Thread t = Thread.currentThread(); TestHelper.scheduleInterrupt(t, 1000); Object lock = new Object();