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 jdk.test.lib.thread;
25
26 import java.lang.reflect.Field;
27 import java.time.Duration;
28 import java.util.concurrent.ForkJoinPool;
29 import java.util.concurrent.atomic.AtomicReference;
30
31 /**
32 * Helper class to support tests running tasks a in virtual thread.
33 */
34 public class VThreadRunner {
35 private VThreadRunner() { }
36
37 /**
38 * Characteristic value signifying that the thread cannot set values for its
39 * copy of thread-locals.
40 */
41 public static final int NO_THREAD_LOCALS = 1 << 1;
42
43 /**
44 * Characteristic value signifying that initial values for inheritable
45 * thread locals are not inherited from the constructing thread.
46 */
47 public static final int NO_INHERIT_THREAD_LOCALS = 1 << 2;
48
49 /**
50 * Represents a task that does not return a result but may throw
51 * an exception.
52 */
53 @FunctionalInterface
54 public interface ThrowingRunnable {
55 /**
56 * Runs this operation.
57 */
58 void run() throws Exception;
59 }
60
61 /**
62 * Run a task in a virtual thread and wait for it to terminate.
68 * @param task the task to run
69 * @throws Exception the exception thrown by the task
70 */
71 public static void run(String name,
72 int characteristics,
73 ThrowingRunnable task) throws Exception {
74 AtomicReference<Exception> exc = new AtomicReference<>();
75 Runnable target = () -> {
76 try {
77 task.run();
78 } catch (Error e) {
79 exc.set(new RuntimeException(e));
80 } catch (Exception e) {
81 exc.set(e);
82 }
83 };
84
85 Thread.Builder builder = Thread.ofVirtual();
86 if (name != null)
87 builder.name(name);
88 if ((characteristics & NO_THREAD_LOCALS) != 0)
89 builder.allowSetThreadLocals(false);
90 if ((characteristics & NO_INHERIT_THREAD_LOCALS) != 0)
91 builder.inheritInheritableThreadLocals(false);
92 Thread thread = builder.start(target);
93
94 // wait for thread to terminate
95 while (thread.join(Duration.ofSeconds(10)) == false) {
96 System.out.println("-- " + thread + " --");
97 for (StackTraceElement e : thread.getStackTrace()) {
98 System.out.println(" " + e);
99 }
100 }
101
102 Exception e = exc.get();
103 if (e != null) {
104 throw e;
105 }
106 }
107
108 /**
109 * Run a task in a virtual thread and wait for it to terminate.
|
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 jdk.test.lib.thread;
25
26 import java.lang.reflect.Field;
27 import java.time.Duration;
28 import java.util.concurrent.ForkJoinPool;
29 import java.util.concurrent.atomic.AtomicReference;
30
31 /**
32 * Helper class to support tests running tasks a in virtual thread.
33 */
34 public class VThreadRunner {
35 private VThreadRunner() { }
36
37 /**
38 * Characteristic value signifying that initial values for inheritable
39 * thread locals are not inherited from the constructing thread.
40 */
41 public static final int NO_INHERIT_THREAD_LOCALS = 1 << 2;
42
43 /**
44 * Represents a task that does not return a result but may throw
45 * an exception.
46 */
47 @FunctionalInterface
48 public interface ThrowingRunnable {
49 /**
50 * Runs this operation.
51 */
52 void run() throws Exception;
53 }
54
55 /**
56 * Run a task in a virtual thread and wait for it to terminate.
62 * @param task the task to run
63 * @throws Exception the exception thrown by the task
64 */
65 public static void run(String name,
66 int characteristics,
67 ThrowingRunnable task) throws Exception {
68 AtomicReference<Exception> exc = new AtomicReference<>();
69 Runnable target = () -> {
70 try {
71 task.run();
72 } catch (Error e) {
73 exc.set(new RuntimeException(e));
74 } catch (Exception e) {
75 exc.set(e);
76 }
77 };
78
79 Thread.Builder builder = Thread.ofVirtual();
80 if (name != null)
81 builder.name(name);
82 if ((characteristics & NO_INHERIT_THREAD_LOCALS) != 0)
83 builder.inheritInheritableThreadLocals(false);
84 Thread thread = builder.start(target);
85
86 // wait for thread to terminate
87 while (thread.join(Duration.ofSeconds(10)) == false) {
88 System.out.println("-- " + thread + " --");
89 for (StackTraceElement e : thread.getStackTrace()) {
90 System.out.println(" " + e);
91 }
92 }
93
94 Exception e = exc.get();
95 if (e != null) {
96 throw e;
97 }
98 }
99
100 /**
101 * Run a task in a virtual thread and wait for it to terminate.
|