< prev index next >

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

Print this page

  1 /*
  2  * Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 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 /**
 25  * @test
 26  * @summary Test virtual threads using core reflection
 27  * @modules java.base/java.lang:+open
 28  * @library /test/lib
 29  * @run junit Reflection
 30  */
 31 
 32 import java.lang.reflect.Constructor;
 33 import java.lang.reflect.InvocationTargetException;
 34 import java.lang.reflect.Method;
 35 import java.util.concurrent.CountDownLatch;
 36 import java.util.concurrent.ExecutorService;
 37 import java.util.concurrent.Executors;
 38 import java.util.concurrent.ThreadFactory;
 39 import java.util.concurrent.locks.LockSupport;
 40 

 41 import jdk.test.lib.thread.VThreadRunner;
 42 import org.junit.jupiter.api.Test;
 43 import static org.junit.jupiter.api.Assertions.*;
 44 import static org.junit.jupiter.api.Assumptions.*;
 45 
 46 class Reflection {
 47 
 48     /**
 49      * Test invoking static method.
 50      */
 51     @Test
 52     void testInvokeStatic1() throws Exception {
 53         VThreadRunner.run(() -> {
 54             int result = (int) divideMethod().invoke(null, 20, 2);
 55             assertTrue(result == 10);
 56         });
 57     }
 58 
 59     /**
 60      * Test that InvocationTargetException is thrown when a static method throws

125     @Test
126     void testInvokeStatic5() throws Exception {
127         VThreadRunner.run(() -> {
128             Method foo = BadClass2.class.getDeclaredMethod("foo");
129             assertThrows(AbstractMethodError.class, () -> foo.invoke(null));
130         });
131     }
132 
133     static class BadClass2 {
134         static {
135             if (1==1) throw new AbstractMethodError();
136         }
137         static void foo() { }
138     }
139 
140     /**
141      * Test that invoking a static method does not pin the carrier thread.
142      */
143     @Test
144     void testInvokeStatic6() throws Exception {
145         assumeTrue(ThreadBuilders.supportsCustomScheduler(), "No support for custom schedulers");
146         Method parkMethod = Parker.class.getDeclaredMethod("park");
147         try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) {
148             Thread.Builder builder = ThreadBuilders.virtualThreadBuilder(scheduler);
149             ThreadFactory factory = builder.factory();
150 
151             var ready = new CountDownLatch(1);
152             Thread vthread = factory.newThread(() -> {
153                 ready.countDown();
154                 try {
155                     parkMethod.invoke(null);   // blocks
156                 } catch (Exception e) { }
157             });
158             vthread.start();
159 
160             try {
161                 // wait for thread to run
162                 ready.await();
163 
164                 // unpark with another virtual thread, runs on same carrier thread
165                 Thread unparker = factory.newThread(() -> LockSupport.unpark(vthread));
166                 unparker.start();
167                 unparker.join();
168             } finally {
169                 LockSupport.unpark(vthread);  // in case test fails

304     @Test
305     void testNewInstance5() throws Exception {
306         VThreadRunner.run(() -> {
307             Constructor<?> ctor = BadClass4.class.getDeclaredConstructor();
308             assertThrows(AbstractMethodError.class, () -> ctor.newInstance((Object[])null));
309         });
310     }
311 
312     static class BadClass4 {
313         static {
314             if (1==1) throw new AbstractMethodError();
315         }
316         static void foo() { }
317     }
318 
319     /**
320      * Test that newInstance does not pin the carrier thread
321      */
322     @Test
323     void testNewInstance6() throws Exception {
324         assumeTrue(ThreadBuilders.supportsCustomScheduler(), "No support for custom schedulers");
325         Constructor<?> ctor = Parker.class.getDeclaredConstructor();
326         try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) {
327             Thread.Builder builder = ThreadBuilders.virtualThreadBuilder(scheduler);
328             ThreadFactory factory = builder.factory();
329 
330             var ready = new CountDownLatch(1);
331             Thread vthread = factory.newThread(() -> {
332                 ready.countDown();
333                 try {
334                     ctor.newInstance();
335                 } catch (Exception e) { }
336             });
337             vthread.start();
338 
339             try {
340                 // wait for thread to run
341                 ready.await();
342 
343                 // unpark with another virtual thread, runs on same carrier thread
344                 Thread unparker = factory.newThread(() -> LockSupport.unpark(vthread));
345                 unparker.start();
346                 unparker.join();
347             } finally {
348                 LockSupport.unpark(vthread);  // in case test fails

  1 /*
  2  * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 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 /**
 25  * @test
 26  * @summary Test virtual threads using core reflection
 27  * @modules java.base/java.lang:+open
 28  * @library /test/lib
 29  * @run junit Reflection
 30  */
 31 
 32 import java.lang.reflect.Constructor;
 33 import java.lang.reflect.InvocationTargetException;
 34 import java.lang.reflect.Method;
 35 import java.util.concurrent.CountDownLatch;
 36 import java.util.concurrent.ExecutorService;
 37 import java.util.concurrent.Executors;
 38 import java.util.concurrent.ThreadFactory;
 39 import java.util.concurrent.locks.LockSupport;
 40 
 41 import jdk.test.lib.thread.VThreadScheduler;
 42 import jdk.test.lib.thread.VThreadRunner;
 43 import org.junit.jupiter.api.Test;
 44 import static org.junit.jupiter.api.Assertions.*;
 45 import static org.junit.jupiter.api.Assumptions.*;
 46 
 47 class Reflection {
 48 
 49     /**
 50      * Test invoking static method.
 51      */
 52     @Test
 53     void testInvokeStatic1() throws Exception {
 54         VThreadRunner.run(() -> {
 55             int result = (int) divideMethod().invoke(null, 20, 2);
 56             assertTrue(result == 10);
 57         });
 58     }
 59 
 60     /**
 61      * Test that InvocationTargetException is thrown when a static method throws

126     @Test
127     void testInvokeStatic5() throws Exception {
128         VThreadRunner.run(() -> {
129             Method foo = BadClass2.class.getDeclaredMethod("foo");
130             assertThrows(AbstractMethodError.class, () -> foo.invoke(null));
131         });
132     }
133 
134     static class BadClass2 {
135         static {
136             if (1==1) throw new AbstractMethodError();
137         }
138         static void foo() { }
139     }
140 
141     /**
142      * Test that invoking a static method does not pin the carrier thread.
143      */
144     @Test
145     void testInvokeStatic6() throws Exception {
146         assumeTrue(VThreadScheduler.supportsCustomScheduler(), "No support for custom schedulers");
147         Method parkMethod = Parker.class.getDeclaredMethod("park");
148         try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) {
149             ThreadFactory factory = VThreadScheduler.virtualThreadFactory(scheduler);

150 
151             var ready = new CountDownLatch(1);
152             Thread vthread = factory.newThread(() -> {
153                 ready.countDown();
154                 try {
155                     parkMethod.invoke(null);   // blocks
156                 } catch (Exception e) { }
157             });
158             vthread.start();
159 
160             try {
161                 // wait for thread to run
162                 ready.await();
163 
164                 // unpark with another virtual thread, runs on same carrier thread
165                 Thread unparker = factory.newThread(() -> LockSupport.unpark(vthread));
166                 unparker.start();
167                 unparker.join();
168             } finally {
169                 LockSupport.unpark(vthread);  // in case test fails

304     @Test
305     void testNewInstance5() throws Exception {
306         VThreadRunner.run(() -> {
307             Constructor<?> ctor = BadClass4.class.getDeclaredConstructor();
308             assertThrows(AbstractMethodError.class, () -> ctor.newInstance((Object[])null));
309         });
310     }
311 
312     static class BadClass4 {
313         static {
314             if (1==1) throw new AbstractMethodError();
315         }
316         static void foo() { }
317     }
318 
319     /**
320      * Test that newInstance does not pin the carrier thread
321      */
322     @Test
323     void testNewInstance6() throws Exception {
324         assumeTrue(VThreadScheduler.supportsCustomScheduler(), "No support for custom schedulers");
325         Constructor<?> ctor = Parker.class.getDeclaredConstructor();
326         try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) {
327             ThreadFactory factory = VThreadScheduler.virtualThreadFactory(scheduler);

328 
329             var ready = new CountDownLatch(1);
330             Thread vthread = factory.newThread(() -> {
331                 ready.countDown();
332                 try {
333                     ctor.newInstance();
334                 } catch (Exception e) { }
335             });
336             vthread.start();
337 
338             try {
339                 // wait for thread to run
340                 ready.await();
341 
342                 // unpark with another virtual thread, runs on same carrier thread
343                 Thread unparker = factory.newThread(() -> LockSupport.unpark(vthread));
344                 unparker.start();
345                 unparker.join();
346             } finally {
347                 LockSupport.unpark(vthread);  // in case test fails
< prev index next >