From 659f32c061cf02d32a8edd6f425a1e893df022bb Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 6 Feb 2020 14:22:28 +0000 Subject: Improve ThreadFinder array handling --- .../src/main/java/me/lucko/spark/common/util/ThreadFinder.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'spark-common/src/main/java/me/lucko/spark') diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/ThreadFinder.java b/spark-common/src/main/java/me/lucko/spark/common/util/ThreadFinder.java index cc0722a..0d1cbd3 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/ThreadFinder.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/ThreadFinder.java @@ -42,7 +42,7 @@ public final class ThreadFinder { // cache the approx active count at the time of construction. // the usages of this class are likely to be somewhat short-lived, so it's good // enough to just cache a value on init. - private final int approxActiveCount = ROOT_THREAD_GROUP.activeCount(); + private int approxActiveCount = ROOT_THREAD_GROUP.activeCount(); /** * Gets a stream of all known active threads. @@ -50,10 +50,12 @@ public final class ThreadFinder { * @return a stream of threads */ public Stream getThreads() { - Thread[] threads = new Thread[this.approxActiveCount + 20]; // +20 to allow a bit of growth for newly created threads - while (ROOT_THREAD_GROUP.enumerate(threads, true) == threads.length) { + Thread[] threads = new Thread[this.approxActiveCount + 10]; // +10 to allow a bit of growth for newly created threads + int len; + while ((len = ROOT_THREAD_GROUP.enumerate(threads, true)) == threads.length) { threads = new Thread[threads.length * 2]; } + this.approxActiveCount = len; return Arrays.stream(threads).filter(Objects::nonNull); } -- cgit