|
37 | 37 | import java.util.concurrent.Future; |
38 | 38 | import java.util.concurrent.ThreadFactory; |
39 | 39 |
|
| 40 | +import org.scijava.Context; |
40 | 41 | import org.scijava.service.SciJavaService; |
41 | 42 |
|
42 | 43 | /** |
|
46 | 47 | */ |
47 | 48 | public interface ThreadService extends SciJavaService, ThreadFactory { |
48 | 49 |
|
| 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 | + |
49 | 70 | /** |
50 | 71 | * Asynchronously executes the given code in a new thread, as decided by the |
51 | 72 | * thread service. Typically this means that the service allocates a thread |
@@ -123,4 +144,22 @@ void invoke(Runnable code) throws InterruptedException, |
123 | 144 | * @return the thread that asked the {@link ThreadService} to spawn the specified thread |
124 | 145 | */ |
125 | 146 | 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 | + |
126 | 165 | } |
0 commit comments