14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.lang;
26
27 import java.lang.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 }
185 if (priority != 0)
186 thread.priority(priority);
187 UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
188 if (uhe != null)
189 thread.uncaughtExceptionHandler(uhe);
190 return thread;
191 }
192
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) {
245 setUncaughtExceptionHandler(ueh);
246 return this;
247 }
248
249 @Override
250 public Thread unstarted(Runnable task) {
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;
419 private boolean runInvoked;
420
421 BoundVirtualThread(String name, int characteristics, Runnable task) {
422 super(name, characteristics, true);
423 this.task = task;
424 }
425
426 @Override
427 public void run() {
428 // run is specified to do nothing when Thread is a virtual thread
429 if (Thread.currentThread() == this && !runInvoked) {
430 runInvoked = true;
|
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.lang;
26
27 import java.lang.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.ThreadFactory;
35 import jdk.internal.misc.Unsafe;
36 import jdk.internal.invoke.MhUtil;
37 import jdk.internal.reflect.CallerSensitive;
38 import jdk.internal.reflect.Reflection;
39 import jdk.internal.vm.ContinuationSupport;
40
41 /**
42 * Defines static methods to create platform and virtual thread builders.
43 */
44 class ThreadBuilders {
45 private ThreadBuilders() { }
46
47 /**
48 * Base class for Thread.Builder implementations.
49 */
50 private static class BaseThreadBuilder {
51 private String name;
52 private long counter;
53 private int characteristics;
54 private UncaughtExceptionHandler uhe;
55
56 String name() {
57 return name;
58 }
186 if (priority != 0)
187 thread.priority(priority);
188 UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
189 if (uhe != null)
190 thread.uncaughtExceptionHandler(uhe);
191 return thread;
192 }
193
194 @Override
195 public Thread start(Runnable task) {
196 Thread thread = unstarted(task);
197 thread.start();
198 return thread;
199 }
200
201 @Override
202 public ThreadFactory factory() {
203 return new PlatformThreadFactory(group, name(), counter(), characteristics(),
204 daemonChanged, daemon, priority, stackSize, uncaughtExceptionHandler());
205 }
206 }
207
208 /**
209 * ThreadBuilder.OfVirtual implementation.
210 */
211 static final class VirtualThreadBuilder
212 extends BaseThreadBuilder implements OfVirtual {
213 private Thread.VirtualThreadScheduler scheduler;
214
215 VirtualThreadBuilder() {
216 }
217
218 @Override
219 public OfVirtual name(String name) {
220 setName(name);
221 return this;
222 }
223
224 @Override
225 public OfVirtual name(String prefix, long start) {
226 setName(prefix, start);
227 return this;
228 }
229
230 @Override
231 public OfVirtual inheritInheritableThreadLocals(boolean inherit) {
232 setInheritInheritableThreadLocals(inherit);
233 return this;
234 }
235
236 @Override
237 public OfVirtual uncaughtExceptionHandler(UncaughtExceptionHandler ueh) {
238 setUncaughtExceptionHandler(ueh);
239 return this;
240 }
241
242 Thread unstarted(Runnable task, Thread preferredCarrier) {
243 Objects.requireNonNull(task);
244 var thread = newVirtualThread(scheduler,
245 preferredCarrier,
246 nextThreadName(),
247 characteristics(),
248 task);
249 UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
250 if (uhe != null)
251 thread.uncaughtExceptionHandler(uhe);
252 return thread;
253 }
254
255 @Override
256 public Thread unstarted(Runnable task) {
257 return unstarted(task, null);
258 }
259
260 @Override
261 public Thread start(Runnable task) {
262 Thread thread = unstarted(task);
263 thread.start();
264 return thread;
265 }
266
267 @Override
268 public ThreadFactory factory() {
269 return new VirtualThreadFactory(scheduler, name(), counter(), characteristics(),
270 uncaughtExceptionHandler());
271 }
272
273 @SuppressWarnings("removal")
274 @CallerSensitive
275 @Override
276 public OfVirtual scheduler(Thread.VirtualThreadScheduler scheduler) {
277 if (!ContinuationSupport.isSupported()) {
278 throw new UnsupportedOperationException();
279 }
280 // can't mix custom default scheduler and API prototypes at this time
281 if (scheduler != VirtualThread.defaultScheduler()
282 && VirtualThread.defaultScheduler() != VirtualThread.builtinScheduler(true)) {
283 throw new UnsupportedOperationException();
284 }
285 Class<?> caller = Reflection.getCallerClass();
286 caller.getModule().ensureNativeAccess(OfVirtual.class, "scheduler", caller, false);
287 this.scheduler = Objects.requireNonNull(scheduler);
288 return this;
289 }
290 }
291
292 /**
293 * Base ThreadFactory implementation.
294 */
295 private abstract static class BaseThreadFactory implements ThreadFactory {
296 private static final VarHandle COUNT = MhUtil.findVarHandle(
297 MethodHandles.lookup(), "count", long.class);
298
299 private final String name;
300 private final int characteristics;
301 private final UncaughtExceptionHandler uhe;
302
303 private final boolean hasCounter;
304 private volatile long count;
305
306 BaseThreadFactory(String name,
307 long start,
308 int characteristics,
309 UncaughtExceptionHandler uhe) {
371 @Override
372 public Thread newThread(Runnable task) {
373 Objects.requireNonNull(task);
374 String name = nextThreadName();
375 Thread thread = new Thread(group, name, characteristics(), task, stackSize);
376 if (daemonChanged)
377 thread.daemon(daemon);
378 if (priority != 0)
379 thread.priority(priority);
380 UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
381 if (uhe != null)
382 thread.uncaughtExceptionHandler(uhe);
383 return thread;
384 }
385 }
386
387 /**
388 * ThreadFactory for virtual threads.
389 */
390 private static class VirtualThreadFactory extends BaseThreadFactory {
391 private final Thread.VirtualThreadScheduler scheduler;
392
393 VirtualThreadFactory(Thread.VirtualThreadScheduler scheduler,
394 String name,
395 long start,
396 int characteristics,
397 UncaughtExceptionHandler uhe) {
398 super(name, start, characteristics, uhe);
399 this.scheduler = scheduler;
400 }
401
402 @Override
403 public Thread newThread(Runnable task) {
404 Objects.requireNonNull(task);
405 String name = nextThreadName();
406 Thread thread = newVirtualThread(scheduler, null, name, characteristics(), task);
407 UncaughtExceptionHandler uhe = uncaughtExceptionHandler();
408 if (uhe != null)
409 thread.uncaughtExceptionHandler(uhe);
410 return thread;
411 }
412 }
413
414 /**
415 * Creates a new virtual thread to run the given task.
416 */
417 private static Thread newVirtualThread(Thread.VirtualThreadScheduler scheduler,
418 Thread preferredCarrier,
419 String name,
420 int characteristics,
421 Runnable task) {
422 if (ContinuationSupport.isSupported()) {
423 return new VirtualThread(scheduler, preferredCarrier, name, characteristics, task);
424 } else {
425 if (scheduler != null)
426 throw new UnsupportedOperationException();
427 return new BoundVirtualThread(name, characteristics, task);
428 }
429 }
430
431 static Thread newVirtualThread(String name, int characteristics, Runnable task) {
432 return newVirtualThread(null, null, name, characteristics, task);
433 }
434
435 /**
436 * A "virtual thread" that is backed by a platform thread. This implementation
437 * is intended for platforms that don't have the underlying VM support for
438 * continuations. It can also be used for testing.
439 */
440 static final class BoundVirtualThread extends BaseVirtualThread {
441 private static final Unsafe U = Unsafe.getUnsafe();
442 private final Runnable task;
443 private boolean runInvoked;
444
445 BoundVirtualThread(String name, int characteristics, Runnable task) {
446 super(name, characteristics, true);
447 this.task = task;
448 }
449
450 @Override
451 public void run() {
452 // run is specified to do nothing when Thread is a virtual thread
453 if (Thread.currentThread() == this && !runInvoked) {
454 runInvoked = true;
|