< prev index next >

src/java.base/share/classes/jdk/internal/vm/ThreadContainer.java

Print this page

  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

 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 }

  1 /*
  2  * Copyright (c) 2021, 2025, 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

 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 {@code add} to add a thread to this container before it starts.
 80      */
 81     protected void onStart(Thread thread) {

 82     }
 83 
 84     /**
 85      * Invoked by {@code remove} to remove a thread from this container when it
 86      * terminates (or failed to start).






 87      */
 88     protected void onExit(Thread thread) {
 89     }
 90 
 91     /**
 92      * Adds a thread to this container. This method should be invoked before the
 93      * thread executes.
 94      */
 95     public final void add(Thread thread) {
 96         // Prevent a virtual thread from being preempted as this could potentially
 97         // deadlock with a carrier is removing a virtual thread from the container
 98         boolean pinned = ContinuationSupport.pinIfSupported();
 99         try {
100             onStart(thread);
101         } finally {
102             if (pinned) Continuation.unpin();
103         }
104     }
105 
106     /**
107      * Remove a thread from this container. This method can be invoked by the thread
108      * itself as it terminates, or it can be invoked by another thread after the given
109      * thread has terminated (or failed to start).
110      */
111     public final void remove(Thread thread) {
112         // Prevent a virtual thread from being preempted as this could potentially
113         // deadlock with a carrier is removing a virtual thread from the container
114         boolean pinned = ContinuationSupport.pinIfSupported();
115         try {
116             onExit(thread);
117         } finally {
118             if (pinned) Continuation.unpin();
119         }
120     }
121 
122     /**
123      * The scoped values captured when the thread container was created.
124      */
125     public ScopedValueContainer.BindingsSnapshot scopedValueBindings() {
126         return null;
127     }
128 
129     @Override
130     public String toString() {
131         String name = name();
132         if (name != null && name.indexOf('@') >= 0) {
133             return name;
134         } else {
135             String id = Objects.toIdentityString(this);
136             return (name != null) ? name + "/" + id : id;
137         }
138     }
139 }
< prev index next >