< prev index next >

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

Print this page

 71     void testCustomScheduler1() throws Exception {
 72         var ref = new AtomicReference<Executor>();
 73         ThreadFactory factory = VThreadScheduler.virtualThreadFactory(scheduler1);
 74         Thread thread = factory.newThread(() -> {
 75             ref.set(VThreadScheduler.scheduler(Thread.currentThread()));
 76         });
 77         thread.start();
 78         thread.join();
 79         assertTrue(ref.get() == scheduler1);
 80     }
 81 
 82     /**
 83      * Test virtual thread creating a virtual thread that uses a custom scheduler.
 84      */
 85     @Test
 86     void testCustomScheduler2() throws Exception {
 87         VThreadRunner.run(this::testCustomScheduler1);
 88     }
 89 
 90     /**
 91      * Test virtual thread using custom scheduler creating a virtual thread.
 92      * The scheduler should be inherited.
 93      */
 94     @Test
 95     void testCustomScheduler3() throws Exception {
 96         var ref = new AtomicReference<Executor>();
 97         ThreadFactory factory = VThreadScheduler.virtualThreadFactory(scheduler1);
 98         Thread thread = factory.newThread(() -> {
 99             try {
100                 Thread.ofVirtual().start(() -> {
101                     ref.set(VThreadScheduler.scheduler(Thread.currentThread()));
102                 }).join();
103             } catch (Exception e) {
104                 e.printStackTrace();
105             }
106         });
107         thread.start();
108         thread.join();
109         assertTrue(ref.get() == scheduler1);
110     }
111 
112     /**
113      * Test virtual thread using custom scheduler creating a virtual thread
114      * that uses a different custom scheduler.
115      */
116     @Test
117     void testCustomScheduler4() throws Exception {
118         var ref = new AtomicReference<Executor>();
119         ThreadFactory factory1 = VThreadScheduler.virtualThreadFactory(scheduler1);
120         ThreadFactory factory2 = VThreadScheduler.virtualThreadFactory(scheduler2);
121         Thread thread1 = factory1.newThread(() -> {
122             try {
123                 Thread thread2 = factory2.newThread(() -> {
124                     ref.set(VThreadScheduler.scheduler(Thread.currentThread()));
125                 });
126                 thread2.start();
127                 thread2.join();
128             } catch (Exception e) {
129                 e.printStackTrace();

 71     void testCustomScheduler1() throws Exception {
 72         var ref = new AtomicReference<Executor>();
 73         ThreadFactory factory = VThreadScheduler.virtualThreadFactory(scheduler1);
 74         Thread thread = factory.newThread(() -> {
 75             ref.set(VThreadScheduler.scheduler(Thread.currentThread()));
 76         });
 77         thread.start();
 78         thread.join();
 79         assertTrue(ref.get() == scheduler1);
 80     }
 81 
 82     /**
 83      * Test virtual thread creating a virtual thread that uses a custom scheduler.
 84      */
 85     @Test
 86     void testCustomScheduler2() throws Exception {
 87         VThreadRunner.run(this::testCustomScheduler1);
 88     }
 89 
 90     /**
 91      * Test virtual thread using custom scheduler creating a virtual thread that uses
 92      * the default scheduler.
 93      */
 94     @Test
 95     void testCustomScheduler3() throws Exception {
 96         var ref = new AtomicReference<Executor>();
 97         ThreadFactory factory = VThreadScheduler.virtualThreadFactory(scheduler1);
 98         Thread thread = factory.newThread(() -> {
 99             try {
100                 Thread.ofVirtual().start(() -> {
101                     ref.set(VThreadScheduler.scheduler(Thread.currentThread()));
102                 }).join();
103             } catch (Exception e) {
104                 e.printStackTrace();
105             }
106         });
107         thread.start();
108         thread.join();
109         assertTrue(ref.get() == VThreadScheduler.defaultScheduler());
110     }
111 
112     /**
113      * Test virtual thread using custom scheduler creating a virtual thread
114      * that uses a different custom scheduler.
115      */
116     @Test
117     void testCustomScheduler4() throws Exception {
118         var ref = new AtomicReference<Executor>();
119         ThreadFactory factory1 = VThreadScheduler.virtualThreadFactory(scheduler1);
120         ThreadFactory factory2 = VThreadScheduler.virtualThreadFactory(scheduler2);
121         Thread thread1 = factory1.newThread(() -> {
122             try {
123                 Thread thread2 = factory2.newThread(() -> {
124                     ref.set(VThreadScheduler.scheduler(Thread.currentThread()));
125                 });
126                 thread2.start();
127                 thread2.join();
128             } catch (Exception e) {
129                 e.printStackTrace();
< prev index next >