< prev index next >

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

Print this page

 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package java.lang;
 26 
 27 import java.lang.Thread.Builder.OfPlatform;
 28 import java.lang.Thread.Builder.OfVirtual;
 29 import java.lang.Thread.UncaughtExceptionHandler;
 30 import java.lang.invoke.MethodHandles;
 31 import java.lang.invoke.VarHandle;
 32 import java.util.Locale;
 33 import java.util.Objects;
 34 import java.util.concurrent.Executor;
 35 import java.util.concurrent.ThreadFactory;
 36 import jdk.internal.misc.Unsafe;
 37 import jdk.internal.invoke.MhUtil;


 38 import jdk.internal.vm.ContinuationSupport;
 39 
 40 /**
 41  * Defines static methods to create platform and virtual thread builders.
 42  */
 43 class ThreadBuilders {
 44     private ThreadBuilders() { }
 45 
 46     /**
 47      * Base class for Thread.Builder implementations.
 48      */
 49     private static class BaseThreadBuilder {
 50         private String name;
 51         private long counter;
 52         private int characteristics;
 53         private UncaughtExceptionHandler uhe;
 54 
 55         String name() {
 56             return name;
 57         }

193         @Override
194         public Thread start(Runnable task) {
195             Thread thread = unstarted(task);
196             thread.start();
197             return thread;
198         }
199 
200         @Override
201         public ThreadFactory factory() {
202             return new PlatformThreadFactory(group, name(), counter(), characteristics(),
203                     daemonChanged, daemon, priority, stackSize, uncaughtExceptionHandler());
204         }
205 
206     }
207 
208     /**
209      * ThreadBuilder.OfVirtual implementation.
210      */
211     static final class VirtualThreadBuilder
212             extends BaseThreadBuilder implements OfVirtual {
213         private Executor scheduler;
214 
215         VirtualThreadBuilder() {
216         }
217 
218         // invoked by tests
219         VirtualThreadBuilder(Executor scheduler) {
220             if (!ContinuationSupport.isSupported())
221                 throw new UnsupportedOperationException();
222             this.scheduler = Objects.requireNonNull(scheduler);
223         }
224 
225         @Override
226         public OfVirtual name(String name) {
227             setName(name);
228             return this;
229         }
230 
231         @Override
232         public OfVirtual name(String prefix, long start) {
233             setName(prefix, start);
234             return this;
235         }
236 
237         @Override
238         public OfVirtual inheritInheritableThreadLocals(boolean inherit) {
239             setInheritInheritableThreadLocals(inherit);
240             return this;
241         }
242 
243         @Override
244         public OfVirtual uncaughtExceptionHandler(UncaughtExceptionHandler ueh) {

251             Objects.requireNonNull(task);
252             var thread = newVirtualThread(scheduler, nextThreadName(), characteristics(), task);
253             UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
254             if (uhe != null)
255                 thread.uncaughtExceptionHandler(uhe);
256             return thread;
257         }
258 
259         @Override
260         public Thread start(Runnable task) {
261             Thread thread = unstarted(task);
262             thread.start();
263             return thread;
264         }
265 
266         @Override
267         public ThreadFactory factory() {
268             return new VirtualThreadFactory(scheduler, name(), counter(), characteristics(),
269                     uncaughtExceptionHandler());
270         }












271     }
272 
273     /**
274      * Base ThreadFactory implementation.
275      */
276     private abstract static class BaseThreadFactory implements ThreadFactory {
277         private static final VarHandle COUNT = MhUtil.findVarHandle(
278                 MethodHandles.lookup(), "count", long.class);
279 
280         private final String name;
281         private final int characteristics;
282         private final UncaughtExceptionHandler uhe;
283 
284         private final boolean hasCounter;
285         private volatile long count;
286 
287         BaseThreadFactory(String name,
288                           long start,
289                           int characteristics,
290                           UncaughtExceptionHandler uhe)  {

352         @Override
353         public Thread newThread(Runnable task) {
354             Objects.requireNonNull(task);
355             String name = nextThreadName();
356             Thread thread = new Thread(group, name, characteristics(), task, stackSize);
357             if (daemonChanged)
358                 thread.daemon(daemon);
359             if (priority != 0)
360                 thread.priority(priority);
361             UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
362             if (uhe != null)
363                 thread.uncaughtExceptionHandler(uhe);
364             return thread;
365         }
366     }
367 
368     /**
369      * ThreadFactory for virtual threads.
370      */
371     private static class VirtualThreadFactory extends BaseThreadFactory {
372         private final Executor scheduler;
373 
374         VirtualThreadFactory(Executor scheduler,
375                              String name,
376                              long start,
377                              int characteristics,
378                              UncaughtExceptionHandler uhe) {
379             super(name, start, characteristics, uhe);
380             this.scheduler = scheduler;
381         }
382 
383         @Override
384         public Thread newThread(Runnable task) {
385             Objects.requireNonNull(task);
386             String name = nextThreadName();
387             Thread thread = newVirtualThread(scheduler, name, characteristics(), task);
388             UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
389             if (uhe != null)
390                 thread.uncaughtExceptionHandler(uhe);
391             return thread;
392         }
393     }
394 
395     /**
396      * Creates a new virtual thread to run the given task.
397      */
398     static Thread newVirtualThread(Executor scheduler,
399                                    String name,
400                                    int characteristics,
401                                    Runnable task) {
402         if (ContinuationSupport.isSupported()) {
403             return new VirtualThread(scheduler, name, characteristics, task);
404         } else {
405             if (scheduler != null)
406                 throw new UnsupportedOperationException();
407             return new BoundVirtualThread(name, characteristics, task);
408         }
409     }
410 
411     /**
412      * A "virtual thread" that is backed by a platform thread. This implementation
413      * is intended for platforms that don't have the underlying VM support for
414      * continuations. It can also be used for testing.
415      */
416     static final class BoundVirtualThread extends BaseVirtualThread {
417         private static final Unsafe U = Unsafe.getUnsafe();
418         private final Runnable task;

 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package java.lang;
 26 
 27 import java.lang.Thread.Builder.OfPlatform;
 28 import java.lang.Thread.Builder.OfVirtual;
 29 import java.lang.Thread.UncaughtExceptionHandler;
 30 import java.lang.invoke.MethodHandles;
 31 import java.lang.invoke.VarHandle;
 32 import java.util.Locale;
 33 import java.util.Objects;
 34 import java.util.concurrent.Executor;
 35 import java.util.concurrent.ThreadFactory;
 36 import jdk.internal.misc.Unsafe;
 37 import jdk.internal.invoke.MhUtil;
 38 import jdk.internal.reflect.CallerSensitive;
 39 import jdk.internal.reflect.Reflection;
 40 import jdk.internal.vm.ContinuationSupport;
 41 
 42 /**
 43  * Defines static methods to create platform and virtual thread builders.
 44  */
 45 class ThreadBuilders {
 46     private ThreadBuilders() { }
 47 
 48     /**
 49      * Base class for Thread.Builder implementations.
 50      */
 51     private static class BaseThreadBuilder {
 52         private String name;
 53         private long counter;
 54         private int characteristics;
 55         private UncaughtExceptionHandler uhe;
 56 
 57         String name() {
 58             return name;
 59         }

195         @Override
196         public Thread start(Runnable task) {
197             Thread thread = unstarted(task);
198             thread.start();
199             return thread;
200         }
201 
202         @Override
203         public ThreadFactory factory() {
204             return new PlatformThreadFactory(group, name(), counter(), characteristics(),
205                     daemonChanged, daemon, priority, stackSize, uncaughtExceptionHandler());
206         }
207 
208     }
209 
210     /**
211      * ThreadBuilder.OfVirtual implementation.
212      */
213     static final class VirtualThreadBuilder
214             extends BaseThreadBuilder implements OfVirtual {
215         private Thread.VirtualThreadScheduler scheduler;
216 
217         VirtualThreadBuilder() {
218         }
219 







220         @Override
221         public OfVirtual name(String name) {
222             setName(name);
223             return this;
224         }
225 
226         @Override
227         public OfVirtual name(String prefix, long start) {
228             setName(prefix, start);
229             return this;
230         }
231 
232         @Override
233         public OfVirtual inheritInheritableThreadLocals(boolean inherit) {
234             setInheritInheritableThreadLocals(inherit);
235             return this;
236         }
237 
238         @Override
239         public OfVirtual uncaughtExceptionHandler(UncaughtExceptionHandler ueh) {

246             Objects.requireNonNull(task);
247             var thread = newVirtualThread(scheduler, nextThreadName(), characteristics(), task);
248             UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
249             if (uhe != null)
250                 thread.uncaughtExceptionHandler(uhe);
251             return thread;
252         }
253 
254         @Override
255         public Thread start(Runnable task) {
256             Thread thread = unstarted(task);
257             thread.start();
258             return thread;
259         }
260 
261         @Override
262         public ThreadFactory factory() {
263             return new VirtualThreadFactory(scheduler, name(), counter(), characteristics(),
264                     uncaughtExceptionHandler());
265         }
266 
267         @CallerSensitive
268         @Override
269         public OfVirtual scheduler(Thread.VirtualThreadScheduler scheduler) {
270             Class<?> caller = Reflection.getCallerClass();
271             caller.getModule().ensureNativeAccess(OfVirtual.class, "scheduler", caller, false);
272             if (!ContinuationSupport.isSupported()) {
273                 throw new UnsupportedOperationException();
274             }
275             this.scheduler = Objects.requireNonNull(scheduler);
276             return this;
277         }
278     }
279 
280     /**
281      * Base ThreadFactory implementation.
282      */
283     private abstract static class BaseThreadFactory implements ThreadFactory {
284         private static final VarHandle COUNT = MhUtil.findVarHandle(
285                 MethodHandles.lookup(), "count", long.class);
286 
287         private final String name;
288         private final int characteristics;
289         private final UncaughtExceptionHandler uhe;
290 
291         private final boolean hasCounter;
292         private volatile long count;
293 
294         BaseThreadFactory(String name,
295                           long start,
296                           int characteristics,
297                           UncaughtExceptionHandler uhe)  {

359         @Override
360         public Thread newThread(Runnable task) {
361             Objects.requireNonNull(task);
362             String name = nextThreadName();
363             Thread thread = new Thread(group, name, characteristics(), task, stackSize);
364             if (daemonChanged)
365                 thread.daemon(daemon);
366             if (priority != 0)
367                 thread.priority(priority);
368             UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
369             if (uhe != null)
370                 thread.uncaughtExceptionHandler(uhe);
371             return thread;
372         }
373     }
374 
375     /**
376      * ThreadFactory for virtual threads.
377      */
378     private static class VirtualThreadFactory extends BaseThreadFactory {
379         private final Thread.VirtualThreadScheduler scheduler;
380 
381         VirtualThreadFactory(Thread.VirtualThreadScheduler scheduler,
382                              String name,
383                              long start,
384                              int characteristics,
385                              UncaughtExceptionHandler uhe) {
386             super(name, start, characteristics, uhe);
387             this.scheduler = scheduler;
388         }
389 
390         @Override
391         public Thread newThread(Runnable task) {
392             Objects.requireNonNull(task);
393             String name = nextThreadName();
394             Thread thread = newVirtualThread(scheduler, name, characteristics(), task);
395             UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
396             if (uhe != null)
397                 thread.uncaughtExceptionHandler(uhe);
398             return thread;
399         }
400     }
401 
402     /**
403      * Creates a new virtual thread to run the given task.
404      */
405     static Thread newVirtualThread(Thread.VirtualThreadScheduler scheduler,
406                                    String name,
407                                    int characteristics,
408                                    Runnable task) {
409         if (ContinuationSupport.isSupported()) {
410             return new VirtualThread(scheduler, name, characteristics, task);
411         } else {
412             if (scheduler != null)
413                 throw new UnsupportedOperationException();
414             return new BoundVirtualThread(name, characteristics, task);
415         }
416     }
417 
418     /**
419      * A "virtual thread" that is backed by a platform thread. This implementation
420      * is intended for platforms that don't have the underlying VM support for
421      * continuations. It can also be used for testing.
422      */
423     static final class BoundVirtualThread extends BaseVirtualThread {
424         private static final Unsafe U = Unsafe.getUnsafe();
425         private final Runnable task;
< prev index next >