< prev index next >

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

Print this page

 28  * @modules java.base/java.lang:+open
 29  * @run junit CustomScheduler
 30  */
 31 
 32 import java.lang.reflect.Field;
 33 import java.time.Duration;
 34 import java.util.ArrayList;
 35 import java.util.List;
 36 import java.util.concurrent.*;
 37 import java.util.concurrent.atomic.AtomicBoolean;
 38 import java.util.concurrent.atomic.AtomicReference;
 39 import java.util.concurrent.locks.LockSupport;
 40 
 41 import org.junit.jupiter.api.Test;
 42 import org.junit.jupiter.api.BeforeAll;
 43 import org.junit.jupiter.api.AfterAll;
 44 import static org.junit.jupiter.api.Assertions.*;
 45 import static org.junit.jupiter.api.Assumptions.*;
 46 
 47 class CustomScheduler {
 48     private static final Executor DEFAULT_SCHEDULER = defaultScheduler();
 49     private static ExecutorService scheduler1;
 50     private static ExecutorService scheduler2;
 51 
 52     @BeforeAll
 53     static void setup() {
 54         scheduler1 = Executors.newFixedThreadPool(1);
 55         scheduler2 = Executors.newFixedThreadPool(1);
 56     }
 57 
 58     @AfterAll
 59     static void shutdown() {
 60         scheduler1.shutdown();
 61         scheduler2.shutdown();
 62     }
 63 
 64     /**
 65      * Test platform thread creating a virtual thread that uses a custom scheduler.
 66      */
 67     @Test
 68     void testCustomScheduler1() throws Exception {

199      * Test running task with the carrier interrupt status set.
200      */
201     @Test
202     void testRunWithInterruptSet() throws Exception {
203         assumeFalse(Thread.currentThread().isVirtual(), "Main thread is a virtual thread");
204         Executor scheduler = (task) -> {
205             Thread.currentThread().interrupt();
206             task.run();
207         };
208         try {
209             AtomicBoolean interrupted = new AtomicBoolean();
210             Thread vthread = ThreadBuilders.virtualThreadBuilder(scheduler).start(() -> {
211                 interrupted.set(Thread.currentThread().isInterrupted());
212             });
213             assertFalse(vthread.isInterrupted());
214         } finally {
215             Thread.interrupted();
216         }
217     }
218 
219     /**
220      * Returns the default scheduler.
221      */
222     private static Executor defaultScheduler() {
223         try {
224             Field defaultScheduler = Class.forName("java.lang.VirtualThread")
225                     .getDeclaredField("DEFAULT_SCHEDULER");
226             defaultScheduler.setAccessible(true);
227             return (Executor) defaultScheduler.get(null);
228         } catch (Exception e) {
229             throw new RuntimeException(e);
230         }
231     }
232 
233     /**
234      * Returns the scheduler for the given virtual thread.
235      */
236     private static Executor scheduler(Thread thread) {
237         if (!thread.isVirtual())
238             throw new IllegalArgumentException("Not a virtual thread");
239         try {
240             Field scheduler = Class.forName("java.lang.VirtualThread")
241                     .getDeclaredField("scheduler");
242             scheduler.setAccessible(true);
243             return (Executor) scheduler.get(thread);
244         } catch (Exception e) {
245             throw new RuntimeException(e);
246         }
247     }
248 }

 28  * @modules java.base/java.lang:+open
 29  * @run junit CustomScheduler
 30  */
 31 
 32 import java.lang.reflect.Field;
 33 import java.time.Duration;
 34 import java.util.ArrayList;
 35 import java.util.List;
 36 import java.util.concurrent.*;
 37 import java.util.concurrent.atomic.AtomicBoolean;
 38 import java.util.concurrent.atomic.AtomicReference;
 39 import java.util.concurrent.locks.LockSupport;
 40 
 41 import org.junit.jupiter.api.Test;
 42 import org.junit.jupiter.api.BeforeAll;
 43 import org.junit.jupiter.api.AfterAll;
 44 import static org.junit.jupiter.api.Assertions.*;
 45 import static org.junit.jupiter.api.Assumptions.*;
 46 
 47 class CustomScheduler {

 48     private static ExecutorService scheduler1;
 49     private static ExecutorService scheduler2;
 50 
 51     @BeforeAll
 52     static void setup() {
 53         scheduler1 = Executors.newFixedThreadPool(1);
 54         scheduler2 = Executors.newFixedThreadPool(1);
 55     }
 56 
 57     @AfterAll
 58     static void shutdown() {
 59         scheduler1.shutdown();
 60         scheduler2.shutdown();
 61     }
 62 
 63     /**
 64      * Test platform thread creating a virtual thread that uses a custom scheduler.
 65      */
 66     @Test
 67     void testCustomScheduler1() throws Exception {

198      * Test running task with the carrier interrupt status set.
199      */
200     @Test
201     void testRunWithInterruptSet() throws Exception {
202         assumeFalse(Thread.currentThread().isVirtual(), "Main thread is a virtual thread");
203         Executor scheduler = (task) -> {
204             Thread.currentThread().interrupt();
205             task.run();
206         };
207         try {
208             AtomicBoolean interrupted = new AtomicBoolean();
209             Thread vthread = ThreadBuilders.virtualThreadBuilder(scheduler).start(() -> {
210                 interrupted.set(Thread.currentThread().isInterrupted());
211             });
212             assertFalse(vthread.isInterrupted());
213         } finally {
214             Thread.interrupted();
215         }
216     }
217 














218     /**
219      * Returns the scheduler for the given virtual thread.
220      */
221     private static Executor scheduler(Thread thread) {
222         if (!thread.isVirtual())
223             throw new IllegalArgumentException("Not a virtual thread");
224         try {
225             Field scheduler = Class.forName("java.lang.VirtualThread")
226                     .getDeclaredField("scheduler");
227             scheduler.setAccessible(true);
228             return (Executor) scheduler.get(thread);
229         } catch (Exception e) {
230             throw new RuntimeException(e);
231         }
232     }
233 }
< prev index next >