1 /*
 2  * Copyright (c) 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.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.management;
27 
28 import jdk.internal.javac.PreviewFeature;
29 import jdk.internal.vm.ThreadContainer;
30 import jdk.internal.vm.ThreadContainers;
31 
32 /**
33  * This class consists exclusively of static methods that operate on threads for the
34  * purposes of monitoring and management.
35  *
36  * @since 21
37  */
38 @PreviewFeature(feature = PreviewFeature.Feature.STRUCTURED_CONCURRENCY)
39 public final class Threads {
40     private Threads() { }
41 
42     /**
43      * Returns a string that describes the current thread's enclosing
44      * {@linkplain java.util.concurrent.StructuredTaskScope##TreeStructure scopes}
45      * or an empty string if there no enclosing scopes.
46      *
47      * <p> If there is a security manager set, and describing an enclosing scope
48      * requires getting the stack trace of its owner thread, then the security
49      * manager's {@code checkPermission} method is called to check the permission
50      * {@code RuntimePermission("getStackTrace")}.
51      *
52      * @implSpec The default implementation includes the name, owner, and owner
53      * stack trace of each enclosing scope.
54      *
55      * @apiNote There is no equivalent at this time to describe the enclosing
56      * scopes of other threads.
57      *
58      * @throws SecurityException if denied by the security manager
59      * @see java.util.concurrent.StructuredTaskScope
60      */
61     public static String currentThreadEnclosingScopes() {
62         StringBuilder sb = new StringBuilder();
63         ThreadContainer container = ThreadContainers.container(Thread.currentThread());
64         Thread owner;
65         while ((owner = container.owner()) != null) {
66             sb.append(container)
67                     .append(", owner=")
68                     .append(owner)
69                     .append(System.lineSeparator());
70             for (StackTraceElement e : owner.getStackTrace()) {
71                 sb.append("    ")
72                         .append(e.toString())
73                         .append(System.lineSeparator());
74             }
75             container = container.parent();
76         }
77         return sb.toString();
78     }
79 }