< 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             if (!ContinuationSupport.isSupported()) {
271                 throw new UnsupportedOperationException();
272             }
273             // can't mix custom default scheduler and API prototypes at this time
274             if (VirtualThread.isCustomDefaultScheduler()
275                     && scheduler != VirtualThread.defaultScheduler()) {
276                 throw new UnsupportedOperationException();
277             }
278             Class<?> caller = Reflection.getCallerClass();
279             caller.getModule().ensureNativeAccess(OfVirtual.class, "scheduler", caller, false);
280             this.scheduler = Objects.requireNonNull(scheduler);
281             return this;
282         }
283     }
284 
285     /**
286      * Base ThreadFactory implementation.
287      */
288     private abstract static class BaseThreadFactory implements ThreadFactory {
289         private static final VarHandle COUNT = MhUtil.findVarHandle(
290                 MethodHandles.lookup(), "count", long.class);
291 
292         private final String name;
293         private final int characteristics;
294         private final UncaughtExceptionHandler uhe;
295 
296         private final boolean hasCounter;
297         private volatile long count;
298 
299         BaseThreadFactory(String name,
300                           long start,
301                           int characteristics,
302                           UncaughtExceptionHandler uhe)  {

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