From 14e9ae8887f242eb6b34f7c061387d74d2513e84 Mon Sep 17 00:00:00 2001 From: catbref Date: Thu, 26 Sep 2019 17:25:05 +0100 Subject: [PATCH] ExecuteProduceConsume no longer spawns a pointless, new thread if we have an excess of non-consuming threads --- .../java/org/qora/utils/ExecuteProduceConsume.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/qora/utils/ExecuteProduceConsume.java b/src/main/java/org/qora/utils/ExecuteProduceConsume.java index 5722752e..237ede76 100644 --- a/src/main/java/org/qora/utils/ExecuteProduceConsume.java +++ b/src/main/java/org/qora/utils/ExecuteProduceConsume.java @@ -107,6 +107,7 @@ public abstract class ExecuteProduceConsume implements Runnable { } try { + // It's possible this might need to become a class instance private volatile boolean canBlock = false; while (true) { @@ -155,7 +156,11 @@ public abstract class ExecuteProduceConsume implements Runnable { ++this.tasksProduced; ++this.consumerCount; - if (!this.hasThreadPending) { + this.logger.trace(() -> String.format("[%d] hasThreadPending: %b, activeThreadCount: %d, consumerCount now: %d", + Thread.currentThread().getId(), this.hasThreadPending, this.activeThreadCount, this.consumerCount)); + + // If we have no thread pending and no excess of threads then we should spawn a fresh thread + if (!this.hasThreadPending && this.activeThreadCount <= this.consumerCount + 1) { this.logger.trace(() -> String.format("[%d] spawning another thread", Thread.currentThread().getId())); this.hasThreadPending = true; @@ -165,6 +170,8 @@ public abstract class ExecuteProduceConsume implements Runnable { this.hasThreadPending = false; this.logger.trace(() -> String.format("[%d] failed to spawn another thread", Thread.currentThread().getId())); } + } else { + this.logger.trace(() -> String.format("[%d] NOT spawning another thread", Thread.currentThread().getId())); } } @@ -176,6 +183,9 @@ public abstract class ExecuteProduceConsume implements Runnable { ++this.tasksConsumed; --this.consumerCount; + this.logger.trace(() -> String.format("[%d] consumerCount now: %d", + Thread.currentThread().getId(), this.consumerCount)); + // Quicker, non-blocking produce next round canBlock = false; }