Skip to content

Commit d54022f

Browse files
committed
Various changes to JobState/WorkerPool to make them more scrutable.
Fixed shutting down threads to no longer spew InterruptedException related logs.
1 parent 6763651 commit d54022f

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

release-notes.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ Version 1.8.0 (unreleased)
1111
of an existing/custom GameSystemManager.
1212
* Added GameSystemsState.getGameSystemManager() and
1313
GameSystemsState.get(class, boolean) methods.
14-
* Added RecurringTaskSystem for executing general tasks once per frame.
14+
* Added RecurringTaskSystem for executing general tasks once per frame.
15+
* Added JobState/WorkerPool.isBusy() to indicate if there are still pending/running
16+
workers.
17+
* Added JobState.getPoolSize() for querying the number of threads in the pool.
18+
* Modified JobState.getQueuedCount()/getActiveCount() to return the live numbers
19+
from WorkerPool instead of the (likely delayed) numbers that the versioned
20+
objects are tracking.
21+
* Fixed an issue where shutdown workers were spewing InterruptedException stack
22+
traces to the logs during shutdown.
1523

1624

1725
Version 1.7.0 (latest)

src/main/java/com/simsilica/thread/JobState.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,29 @@ public boolean isQueued( Job job ) {
141141
return workers.isQueued(job);
142142
}
143143

144+
/**
145+
* Returns the pool size that was set for the worker pool.
146+
*/
147+
public int getPoolSize() {
148+
return workers.getPoolSize();
149+
}
150+
151+
/**
152+
* Returns true if the worker pool has any pending work to do
153+
* or is in the middle of doing that work.
154+
*/
155+
public boolean isBusy() {
156+
return workers.isBusy();
157+
}
158+
144159
/**
145160
* Returns the current count of jobs waiting to be run.
146161
*/
147162
public int getQueuedCount() {
148-
return queuedCount.getObject();
163+
//return queuedCount.getObject();
164+
// The holder could be as much as a full frame delayed
165+
// so we'll query the workers directly.
166+
return workers.getQueuedJobCount();
149167
}
150168

151169
/**
@@ -161,7 +179,10 @@ public VersionedReference<Integer> createQueuedCountReference() {
161179
* a thread or waiting to be 'finished'.
162180
*/
163181
public int getActiveCount() {
164-
return activeCount.getObject();
182+
//return activeCount.getObject();
183+
// The holder could be as much as a full frame delayed
184+
// so we'll query the workers directly.
185+
return workers.getActiveJobCount();
165186
}
166187

167188
/**

src/main/java/com/simsilica/thread/WorkerPool.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
import org.slf4j.*;
4444

45+
import com.google.common.base.Throwables;
46+
4547
/**
4648
* Manages a thread pool that can be used to run Job objects that
4749
* have a two phase execution: 1) run on a background thread, 2)
@@ -81,6 +83,8 @@ public class WorkerPool {
8183
private AtomicInteger activeCount = new AtomicInteger(0);
8284

8385
private AtomicLong errorCount = new AtomicLong(0);
86+
87+
private boolean shuttingDown = false;
8488

8589
/**
8690
* Creates a worker pool with 4 worker threads.
@@ -208,6 +212,14 @@ public int getActiveJobCount() {
208212
return activeCount.get();
209213
}
210214

215+
/**
216+
* Returns true if the worker pool has any pending work to do
217+
* or is in the middle of doing that work.
218+
*/
219+
public boolean isBusy() {
220+
return (getActiveJobCount() + getQueuedJobCount()) > 0;
221+
}
222+
211223
/**
212224
* Performs an immediate shutdown where all active threads are
213225
* interrupted and any jobs queued are not executed. Jobs waiting
@@ -216,6 +228,7 @@ public int getActiveJobCount() {
216228
* have completed.
217229
*/
218230
public void shutdownNow( boolean awaitTermination ) {
231+
shuttingDown = true;
219232
workers.shutdownNow();
220233
if( awaitTermination ) {
221234
log.info("Waiting for thread pool shutdown");
@@ -313,9 +326,13 @@ public void run() {
313326
try {
314327
job.runOnWorker();
315328
} catch( Exception e ) {
316-
log.error("Error running job:" + job, e);
317-
activeCount.decrementAndGet();
318-
errorCount.incrementAndGet();
329+
if( shuttingDown && Throwables.getRootCause(e) instanceof InterruptedException ) {
330+
log.info("Thread interrupted successfully");
331+
} else {
332+
log.error("Error running job:" + job, e);
333+
activeCount.decrementAndGet();
334+
errorCount.incrementAndGet();
335+
}
319336
return;
320337
}
321338
if( log.isTraceEnabled() ) {

0 commit comments

Comments
 (0)