< prev index next >

test/jdk/java/lang/Thread/virtual/ThreadBuilders.java

Print this page

34 class ThreadBuilders {
35     private ThreadBuilders() { }
36 
37     private static final Constructor<?> VTBUILDER_CTOR;
38     static {
39         try {
40             Class<?> clazz = Class.forName("java.lang.ThreadBuilders$VirtualThreadBuilder");
41             Constructor<?> ctor = clazz.getDeclaredConstructor(Executor.class);
42             ctor.setAccessible(true);
43             VTBUILDER_CTOR = ctor;
44         } catch (Exception e) {
45             throw new InternalError(e);
46         }
47     }
48 
49     /**
50      * Returns a builder to create virtual threads that use the given scheduler.
51      * @throws UnsupportedOperationException if custom schedulers are not supported
52      */
53     static Thread.Builder.OfVirtual virtualThreadBuilder(Executor scheduler) {
54         Thread.Builder.OfVirtual builder = Thread.ofVirtual();
55         try {
56             return (Thread.Builder.OfVirtual) VTBUILDER_CTOR.newInstance(scheduler);
57         } catch (InvocationTargetException e) {
58             Throwable cause = e.getCause();
59             if (cause instanceof RuntimeException re) {
60                 throw re;
61             }
62             throw new RuntimeException(e);
63         } catch (Exception e) {
64             throw new RuntimeException(e);
65         }
66     }
67 
68     /**
69      * Return true if custom schedulers are supported.
70      */
71     static boolean supportsCustomScheduler() {
72         try (var pool = Executors.newCachedThreadPool()) {
73             try {
74                 virtualThreadBuilder(pool);

34 class ThreadBuilders {
35     private ThreadBuilders() { }
36 
37     private static final Constructor<?> VTBUILDER_CTOR;
38     static {
39         try {
40             Class<?> clazz = Class.forName("java.lang.ThreadBuilders$VirtualThreadBuilder");
41             Constructor<?> ctor = clazz.getDeclaredConstructor(Executor.class);
42             ctor.setAccessible(true);
43             VTBUILDER_CTOR = ctor;
44         } catch (Exception e) {
45             throw new InternalError(e);
46         }
47     }
48 
49     /**
50      * Returns a builder to create virtual threads that use the given scheduler.
51      * @throws UnsupportedOperationException if custom schedulers are not supported
52      */
53     static Thread.Builder.OfVirtual virtualThreadBuilder(Executor scheduler) {

54         try {
55             return (Thread.Builder.OfVirtual) VTBUILDER_CTOR.newInstance(scheduler);
56         } catch (InvocationTargetException e) {
57             Throwable cause = e.getCause();
58             if (cause instanceof RuntimeException re) {
59                 throw re;
60             }
61             throw new RuntimeException(e);
62         } catch (Exception e) {
63             throw new RuntimeException(e);
64         }
65     }
66 
67     /**
68      * Return true if custom schedulers are supported.
69      */
70     static boolean supportsCustomScheduler() {
71         try (var pool = Executors.newCachedThreadPool()) {
72             try {
73                 virtualThreadBuilder(pool);
< prev index next >