< prev index next >

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

Print this page

14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package nsk.share.jdi;
25 
26 import java.util.concurrent.ThreadFactory;
27 
28 /*
29     This factory is used to run new threads in debuggee in JDI tests.
30  */
31 
32 public class JDIThreadFactory {
33 
34     private static ThreadFactory threadFactory = r -> new Thread(r);

35 
36     public static Thread newThread(NamedTask task) {
37         return newThread(task, task.getName());
38     }
39 
40 
41     public static Thread newThread(Runnable task) {
42         return threadFactory.newThread(task);
43     }
44 
45     public static Thread newThread(Runnable task, String name) {
46         Thread t = threadFactory.newThread(task);
47         t.setName(name);
48         return t;
49     }

















50 }

14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package nsk.share.jdi;
25 
26 import java.util.concurrent.ThreadFactory;
27 
28 /*
29     This factory is used to run new threads in debuggee in JDI tests.
30  */
31 
32 public class JDIThreadFactory {
33 
34     private static ThreadFactory threadFactory = "Virtual".equals(System.getProperty("main.wrapper"))
35             ? virtualThreadFactory() : platformThreadFactory();
36 
37     public static Thread newThread(NamedTask task) {
38         return newThread(task, task.getName());
39     }
40 

41     public static Thread newThread(Runnable task) {
42         return threadFactory.newThread(task);
43     }
44 
45     public static Thread newThread(Runnable task, String name) {
46         Thread t = threadFactory.newThread(task);
47         t.setName(name);
48         return t;
49     }
50 
51     private static ThreadFactory platformThreadFactory() {
52         return task -> new Thread(task);
53     }
54 
55     private static ThreadFactory virtualThreadFactory() {
56         try {
57             Object builder = Thread.class.getMethod("ofVirtual").invoke(null);
58             Class<?> clazz = Class.forName("java.lang.Thread$Builder");
59             java.lang.reflect.Method factory = clazz.getMethod("factory");
60             return (ThreadFactory) factory.invoke(builder);
61         } catch (RuntimeException | Error e) {
62             throw e;
63         } catch (Exception e) {
64             throw new RuntimeException(e);
65         }
66     }
67 }
< prev index next >