1 /*
 2  * Copyright (c) 2021, 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.stream.Stream;
29 
30 /**
31  * A container of threads.
32  */
33 public abstract class ThreadContainer extends StackableScope {
34 
35     /**
36      * Creates a ThreadContainer owned by the current thread.
37      */
38     protected ThreadContainer() {
39         super(false);
40     }
41 
42     /**
43      * Creates a ThreadContainer.
44      * @param shared true for a shared container, false for a container
45      * owned by the current thread
46      */
47     ThreadContainer(boolean shared) {
48         super(shared);
49     }
50 
51     /**
52      * Return the container name, null if not named.
53      */
54     public abstract String name();
55 
56     /**
57      * Returns the parent of this container or null if this is the root container.
58      */
59     public ThreadContainer parent() {
60         return ThreadContainers.parent(this);
61     }
62 
63     /**
64      * Return the stream of children of this container.
65      */
66     public final Stream<ThreadContainer> children() {
67         return ThreadContainers.children(this);
68     }
69     
70     /**
71      * Return a count of the number of threads in this container.
72      */
73     public long threadCount() {
74         return threads().mapToLong(e -> 1L).sum();
75     }
76 
77     /**
78      * Returns a stream of the threads in this container.
79      */
80     public abstract Stream<Thread> threads();
81 
82     /**
83      * Invoked when a thread is started in the container
84      */
85     public abstract void onStart(Thread thread);
86 
87     /**
88      * Invoked when thread in container terminates.
89      */
90     public abstract void onExit(Thread thread);
91 
92     /**
93      * The scope locals captured when the thread container was created.
94      */
95     public ScopeLocalContainer.BindingsSnapshot scopeLocalBindings() {
96         return null;
97     }
98 }