Skip to content

Commit 6977b52

Browse files
committed
ThreadService: add API for backing ExecutorService
For some uses cases (particularly for certain calls to ImgLib2 functionality), it is helpful to have access to the ExecutorService being used internally by the ThreadService. And while we're at it, we may as well make the ExecutorService itself configurable. I have a vague intuition that ExecutorService is probably not applicable to all threading models of all possible sorts of Java runtimes, so I include a caveat in the javadoc that the accessor is allowed to return null, and the mutator is allowed to throw UnsupportedOperationException, if the ThreadService implementation does not use an ExecutorService.
1 parent 6dbec39 commit 6977b52

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ public Future<?> run(final Runnable code) {
8383
return executor().submit(wrap(code));
8484
}
8585

86+
@Override
87+
public ExecutorService getExecutorService() {
88+
return executor();
89+
}
90+
91+
@Override
92+
public void setExecutorService(final ExecutorService executor) {
93+
this.executor = executor;
94+
}
95+
8696
@Override
8797
public boolean isDispatchThread() {
8898
return EventQueue.isDispatchThread();

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import java.lang.reflect.InvocationTargetException;
3535
import java.util.concurrent.Callable;
36+
import java.util.concurrent.ExecutorService;
3637
import java.util.concurrent.Future;
3738
import java.util.concurrent.ThreadFactory;
3839

@@ -91,6 +92,24 @@ public enum ThreadContext {
9192
*/
9293
Future<?> run(Runnable code);
9394

95+
/**
96+
* Gets the {@link ExecutorService} object used when {@link #run} is called.
97+
*
98+
* @return the {@link ExecutorService}, or null if an {@link ExecutorService}
99+
* is not used in this {@link ThreadService} implementation.
100+
*/
101+
ExecutorService getExecutorService();
102+
103+
/**
104+
* Sets the {@link ExecutorService} object used when {@link #run} is called.
105+
*
106+
* @param executor The {@link ExecutorService} for this {@link ThreadService}
107+
* to use internally for {@link #run} calls.
108+
* @throws UnsupportedOperationException if this {@link ThreadService} does
109+
* not use an {@link ExecutorService} to handle {@link #run} calls.
110+
*/
111+
void setExecutorService(ExecutorService executor);
112+
94113
/**
95114
* Gets whether the current thread is a dispatch thread for use with
96115
* {@link #invoke} and {@link #queue}.

0 commit comments

Comments
 (0)