diff options
author | Bjørn Christian Seime <bjorncs@yahooinc.com> | 2022-07-26 13:59:42 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@yahooinc.com> | 2022-07-26 14:00:02 +0200 |
commit | d8e1354b4631be8a701db4e52721bfa780f9d384 (patch) | |
tree | 54fbdf45ed7929372d8b0c7b0249a3ce3c1908ed /vespajlib | |
parent | 58a7c193360e6aca3da295592f8b5ab5fcf6424e (diff) |
Add helper to get all threads in JVM
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/concurrent/Threads.java | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/Threads.java b/vespajlib/src/main/java/com/yahoo/concurrent/Threads.java new file mode 100644 index 00000000000..d30750692e9 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/concurrent/Threads.java @@ -0,0 +1,30 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.concurrent; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +/** + * @author bjorncs + */ +public class Threads { + + private Threads() {} + + /** Returns all threads in JVM */ + public static Collection<Thread> getAllThreads() { + ThreadGroup root = Thread.currentThread().getThreadGroup(); + ThreadGroup parent; + while ((parent = root.getParent()) != null) { + root = parent; + } + // The number of threads may increase between activeCount() and enumerate() + Thread[] threads = new Thread[root.activeCount() + 100]; + int count; + while ((count = root.enumerate(threads, true)) == threads.length) { + threads = new Thread[threads.length + 1000]; + } + return List.of(Arrays.copyOf(threads, count)); + } +} |