1 /*
 2  * Copyright (c) 2021, 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 Thread::getStackTrace on a virtual thread that is runnable-unmounted
27  * @requires vm.continuations
28  * @run main/othervm -Djdk.virtualThreadScheduler.maxPoolSize=1 GetStackTraceWhenRunnable
29  */
30 
31 import java.io.IOException;
32 import java.util.concurrent.CountDownLatch;
33 import java.util.concurrent.atomic.AtomicBoolean;
34 import java.util.concurrent.locks.LockSupport;
35 
36 public class GetStackTraceWhenRunnable {
37 
38     public static void main(String[] args) throws Exception {
39 
40         // start thread1 and wait for it to park
41         Thread thread1 = Thread.startVirtualThread(LockSupport::park);
42         while (thread1.getState() != Thread.State.WAITING) {
43             Thread.sleep(20);
44         }
45 
46         // start thread2 to pin the carrier thread
47         var started = new AtomicBoolean();
48         var done = new AtomicBoolean();
49         Thread thread2 = Thread.startVirtualThread(() -> {
50             started.set(true);
51             while (!done.get()) {
52                 Thread.onSpinWait();
53             }
54         });
55         try {
56             // wait for thread2 to start
57             while (!started.get()) {
58                 Thread.sleep(10);
59             }
60 
61             // unpark thread1 and check that it is "stuck" in the runnable state
62             // (the carrier thread is pinned, no other virtual thread can run)
63             LockSupport.unpark(thread1);
64             for (int i = 0; i < 5; i++) {
65                 assertTrue(thread1.getState() == Thread.State.RUNNABLE);
66                 Thread.sleep(100);
67             }
68 
69             // print thread1's stack trace
70             StackTraceElement[] stack = thread1.getStackTrace();
71             assertTrue(stack.length > 0);
72             for (StackTraceElement e : stack) {
73                 System.out.println(e);
74             }
75         } finally {
76             done.set(true);
77             thread2.join();
78             thread1.join();
79         }
80     }
81 
82     static void assertTrue(boolean e) {
83         if (!e) throw new RuntimeException();
84     }
85 }