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.  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 jdk.internal.vm;
 27 
 28 import java.util.Objects;
 29 import java.util.stream.Stream;
 30 
 31 /**
 32  * A container of threads.
 33  */
 34 public abstract class ThreadContainer extends StackableScope {
 35 
 36     /**
 37      * Creates a ThreadContainer.
 38      * @param shared true for a shared container, false for a container
 39      * owned by the current thread
 40      */
 41     protected ThreadContainer(boolean shared) {
 42         super(shared);
 43     }
 44 
 45     /**
 46      * Return the name of this container, may be null.
 47      */
 48     public String name() {
 49         return null;
 50     }
 51 
 52     /**
 53      * Returns the parent of this container or null if this is the root container.
 54      */
 55     public ThreadContainer parent() {
 56         return ThreadContainers.parent(this);
 57     }
 58 
 59     /**
 60      * Return the stream of children of this container.
 61      */
 62     public final Stream<ThreadContainer> children() {
 63         return ThreadContainers.children(this);
 64     }
 65 
 66     /**
 67      * Return a count of the number of threads in this container.
 68      */
 69     public long threadCount() {
 70         return threads().mapToLong(e -> 1L).sum();
 71     }
 72 
 73     /**
 74      * Returns a stream of the live threads in this container.
 75      */
 76     public abstract Stream<Thread> threads();
 77 
 78     /**
 79      * Invoked by Thread::start before the given Thread is started.
 80      */
 81     public void onStart(Thread thread) {
 82         // do nothing
 83     }
 84 
 85     /**
 86      * Invoked when a Thread terminates or starting it fails.
 87      *
 88      * For a platform thread, this method is invoked by the thread itself when it
 89      * terminates. For a virtual thread, this method is invoked on its carrier
 90      * after the virtual thread has terminated.
 91      *
 92      * If starting the Thread failed then this method is invoked on the thread
 93      * that invoked onStart.
 94      */
 95     public void onExit(Thread thread) {
 96         // do nothing
 97     }
 98 
 99     /**
100      * The scoped values captured when the thread container was created.
101      */
102     public ScopedValueContainer.BindingsSnapshot scopedValueBindings() {
103         return null;
104     }
105 
106     @Override
107     public String toString() {
108         String name = name();
109         if (name != null && name.indexOf('@') >= 0) {
110             return name;
111         } else {
112             String id = Objects.toIdentityString(this);
113             return (name != null) ? name + "/" + id : id;
114         }
115     }
116 }