Skip to content

Commit 0da9121

Browse files
committed
ThreadService: add getThreadContext method
This method tells you whether the given thread was spawned by the same SciJava Context as the ThreadService.
1 parent e99ea09 commit 0da9121

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ public Thread getParent(final Thread thread) {
111111
return parents.get(thread != null ? thread : Thread.currentThread());
112112
}
113113

114+
@Override
115+
public ThreadContext getThreadContext(final Thread thread) {
116+
final String name = thread.getName();
117+
118+
// check for same context
119+
if (name.startsWith(contextThreadPrefix())) return ThreadContext.SAME;
120+
121+
// check for different context
122+
if (name.startsWith(SCIJAVA_THREAD_PREFIX)) return ThreadContext.OTHER;
123+
124+
// recursively check parent thread
125+
final Thread parent = getParent(thread);
126+
if (parent == thread || parent == null) return ThreadContext.NONE;
127+
return getThreadContext(parent);
128+
}
129+
114130
// -- Disposable methods --
115131

116132
@Override

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.concurrent.Future;
3838
import java.util.concurrent.ThreadFactory;
3939

40+
import org.scijava.Context;
4041
import org.scijava.service.SciJavaService;
4142

4243
/**
@@ -46,6 +47,26 @@
4647
*/
4748
public interface ThreadService extends SciJavaService, ThreadFactory {
4849

50+
public enum ThreadContext {
51+
/**
52+
* The thread was spawned by this thread service; i.e., it belongs to the
53+
* same {@link Context}.
54+
*/
55+
SAME,
56+
57+
/**
58+
* The thread was spawned by a SciJava thread service, but not this one;
59+
* i.e., it belongs to a different {@link Context}.
60+
*/
61+
OTHER,
62+
63+
/**
64+
* The thread was not spawned via a SciJava thread service, and its
65+
* {@link Context} is unknown or inapplicable.
66+
*/
67+
NONE
68+
}
69+
4970
/**
5071
* Asynchronously executes the given code in a new thread, as decided by the
5172
* thread service. Typically this means that the service allocates a thread
@@ -123,4 +144,22 @@ void invoke(Runnable code) throws InterruptedException,
123144
* @return the thread that asked the {@link ThreadService} to spawn the specified thread
124145
*/
125146
Thread getParent(Thread thread);
147+
148+
/**
149+
* Analyzes the {@link Context} of the given thread.
150+
*
151+
* @param thread The thread to analyze.
152+
* @return Information about the thread's {@link Context}. Either:
153+
* <ul>
154+
* <li>{@link ThreadContext#SAME} - The thread was spawned by this
155+
* very thread service, and thus shares the same {@link Context}.</li>
156+
* <li>{@link ThreadContext#OTHER} - The thread was spawned by a
157+
* different thread service, and thus has a different {@link Context}.
158+
* </li>
159+
* <li>{@link ThreadContext#NONE} - It is unknown what spawned the
160+
* thread, so it is effectively {@link Context}-free.</li>
161+
* </ul>
162+
*/
163+
ThreadContext getThreadContext(Thread thread);
164+
126165
}

0 commit comments

Comments
 (0)