< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/share/jdi/JDIThreadFactory.java

Print this page
@@ -29,22 +29,39 @@
      This factory is used to run new threads in debuggee in JDI tests.
   */
  
  public class JDIThreadFactory {
  
-     private static ThreadFactory threadFactory = r -> new Thread(r);
+     private static ThreadFactory threadFactory = "Virtual".equals(System.getProperty("main.wrapper"))
+             ? virtualThreadFactory() : platformThreadFactory();
  
      public static Thread newThread(NamedTask task) {
          return newThread(task, task.getName());
      }
  
- 
      public static Thread newThread(Runnable task) {
          return threadFactory.newThread(task);
      }
  
      public static Thread newThread(Runnable task, String name) {
          Thread t = threadFactory.newThread(task);
          t.setName(name);
          return t;
      }
+ 
+     private static ThreadFactory platformThreadFactory() {
+         return task -> new Thread(task);
+     }
+ 
+     private static ThreadFactory virtualThreadFactory() {
+         try {
+             Object builder = Thread.class.getMethod("ofVirtual").invoke(null);
+             Class<?> clazz = Class.forName("java.lang.Thread$Builder");
+             java.lang.reflect.Method factory = clazz.getMethod("factory");
+             return (ThreadFactory) factory.invoke(builder);
+         } catch (RuntimeException | Error e) {
+             throw e;
+         } catch (Exception e) {
+             throw new RuntimeException(e);
+         }
+     }
  }
< prev index next >