Skip to content

Commit 0f6f916

Browse files
committed
DefaultThreadService: let running tasks finish
We wait up to 5 seconds for them to terminate. I don't really have a good idea whether this is a good amount of time, but it should be more than enough for common scenarios. At some point, the ThreadService interface could gain methods to override this value, but for now, we hardcode it.
1 parent 174e05e commit 0f6f916

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/main/java/org/scijava/thread/DefaultThreadService.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.concurrent.Executors;
4040
import java.util.concurrent.Future;
4141
import java.util.concurrent.ThreadFactory;
42+
import java.util.concurrent.TimeUnit;
4243

4344
import org.scijava.log.LogService;
4445
import org.scijava.plugin.Parameter;
@@ -58,6 +59,8 @@ public final class DefaultThreadService extends AbstractService implements
5859

5960
private static final String SCIJAVA_THREAD_PREFIX = "SciJava-";
6061

62+
private static final long SHUTDOWN_TIMEOUT = 5000;
63+
6164
private static WeakHashMap<Thread, Thread> parents =
6265
new WeakHashMap<>();
6366

@@ -159,11 +162,23 @@ public synchronized void dispose() {
159162
disposed = true;
160163
if (executor != null) {
161164
executor.shutdown();
165+
try {
166+
executor.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS);
167+
}
168+
catch (final InterruptedException exc) {
169+
log.debug(exc);
170+
}
162171
executor = null;
163172
}
164173
if (queues != null) {
165174
for (final ExecutorService queue : queues.values()) {
166175
queue.shutdown();
176+
try {
177+
queue.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS);
178+
}
179+
catch (final InterruptedException exc) {
180+
log.debug(exc);
181+
}
167182
}
168183
}
169184
}

0 commit comments

Comments
 (0)