aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko/spark
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2020-02-06 14:22:28 +0000
committerLuck <git@lucko.me>2020-02-06 14:22:28 +0000
commit659f32c061cf02d32a8edd6f425a1e893df022bb (patch)
treec949a3b8b460e072463152fd9d02951c1e9af485 /spark-common/src/main/java/me/lucko/spark
parentef810759468c84668136932bdddf2c04db09fd08 (diff)
downloadspark-659f32c061cf02d32a8edd6f425a1e893df022bb.tar.gz
spark-659f32c061cf02d32a8edd6f425a1e893df022bb.tar.bz2
spark-659f32c061cf02d32a8edd6f425a1e893df022bb.zip
Improve ThreadFinder array handling
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark')
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/util/ThreadFinder.java8
1 files changed, 5 insertions, 3 deletions
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<Thread> 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);
}