Package org.scijava.thread
Interface ThreadService
-
- All Superinterfaces:
Comparable<Prioritized>,Contextual,Disposable,HasPluginInfo,Identifiable,Initializable,Locatable,Logged,Prioritized,RichPlugin,SciJavaPlugin,SciJavaService,Service,ThreadFactory,Versioned
- All Known Implementing Classes:
DefaultThreadService
public interface ThreadService extends SciJavaService, ThreadFactory
Interface for the thread handling service.- Author:
- Curtis Rueden
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classThreadService.ThreadContext
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description ExecutorServicegetExecutorService()Gets theExecutorServiceobject used whenrun(java.util.concurrent.Callable<V>)is called.ThreadgetParent(Thread thread)Returns the thread that called the specified thread.ThreadService.ThreadContextgetThreadContext(Thread thread)Analyzes theContextof the given thread.voidinvoke(Runnable code)Executes the given code in a special dispatch thread, blocking until execution is complete.booleanisDispatchThread()Gets whether the current thread is a dispatch thread for use withinvoke(Runnable)andqueue(Runnable).voidqueue(Runnable code)Queues the given code for later execution in a special dispatch thread, returning immediately.Future<?>queue(String id, Runnable code)Queues the given code for later execution in a dispatch thread associated with the specified ID, returning immediately.<V> Future<V>queue(String id, Callable<V> code)Queues the given code for later execution in a dispatch thread associated with the specified ID, returning immediately.Future<?>run(Runnable code)Asynchronously executes the given code in a new thread, as decided by the thread service.<V> Future<V>run(Callable<V> code)Asynchronously executes the given code in a new thread, as decided by the thread service.voidsetExecutorService(ExecutorService executor)Sets theExecutorServiceobject used whenrun(java.util.concurrent.Callable<V>)is called.-
Methods inherited from interface org.scijava.Contextual
context, getContext, setContext
-
Methods inherited from interface org.scijava.Disposable
dispose
-
Methods inherited from interface org.scijava.plugin.HasPluginInfo
getInfo, setInfo
-
Methods inherited from interface org.scijava.Locatable
getLocation
-
Methods inherited from interface org.scijava.Prioritized
compareTo, getPriority, setPriority
-
Methods inherited from interface org.scijava.plugin.RichPlugin
getIdentifier, log
-
Methods inherited from interface org.scijava.service.Service
initialize, registerEventHandlers
-
Methods inherited from interface java.util.concurrent.ThreadFactory
newThread
-
Methods inherited from interface org.scijava.Versioned
getVersion
-
-
-
-
Method Detail
-
run
<V> Future<V> run(Callable<V> code)
Asynchronously executes the given code in a new thread, as decided by the thread service. Typically this means that the service allocates a thread from its pool, but ultimately the behavior is implementation-dependent. This method returns immediately.- Parameters:
code- The code to execute.- Returns:
- A
Futurethat will contain the result once the execution has finished. CallFuture.get()to access to the return value (which will block until execution has completed).
-
run
Future<?> run(Runnable code)
Asynchronously executes the given code in a new thread, as decided by the thread service. Typically this means that the service allocates a thread from its pool, but ultimately the behavior is implementation-dependent. This method returns immediately.- Parameters:
code- The code to execute.- Returns:
- A
Futurethat can be used to block until the execution has finished. CallFuture.get()to do so.
-
getExecutorService
ExecutorService getExecutorService()
Gets theExecutorServiceobject used whenrun(java.util.concurrent.Callable<V>)is called.- Returns:
- the
ExecutorService, or null if anExecutorServiceis not used in thisThreadServiceimplementation.
-
setExecutorService
void setExecutorService(ExecutorService executor)
Sets theExecutorServiceobject used whenrun(java.util.concurrent.Callable<V>)is called.- Parameters:
executor- TheExecutorServicefor thisThreadServiceto use internally forrun(java.util.concurrent.Callable<V>)calls.- Throws:
UnsupportedOperationException- if thisThreadServicedoes not use anExecutorServiceto handlerun(java.util.concurrent.Callable<V>)calls.
-
isDispatchThread
boolean isDispatchThread()
Gets whether the current thread is a dispatch thread for use withinvoke(Runnable)andqueue(Runnable).In the case of AWT-based applications (e.g., Java on the desktop), this is typically the AWT Event Dispatch Thread (EDT). However, ultimately the behavior is implementation-dependent.
- Returns:
- True iff the current thread is considered a dispatch thread.
-
invoke
void invoke(Runnable code) throws InterruptedException, InvocationTargetException
Executes the given code in a special dispatch thread, blocking until execution is complete.In the case of AWT-based applications (e.g., Java on the desktop), this is typically the AWT Event Dispatch Thread (EDT). However, ultimately the behavior is implementation-dependent.
- Parameters:
code- The code to execute.- Throws:
InterruptedException- If the code execution is interrupted.InvocationTargetException- If an uncaught exception occurs in the code during execution.
-
queue
void queue(Runnable code)
Queues the given code for later execution in a special dispatch thread, returning immediately.In the case of AWT-based applications (e.g., Java on the desktop), this is typically the AWT Event Dispatch Thread (EDT). However, ultimately the behavior is implementation-dependent.
- Parameters:
code- The code to execute.
-
queue
Future<?> queue(String id, Runnable code)
Queues the given code for later execution in a dispatch thread associated with the specified ID, returning immediately.- Parameters:
id- The ID designating which dispatch thread will execute the code.code- The code to execute.- Returns:
- A
FuturewhoseFuture.get()method blocks until the queued code has completed executing and returnsnull. - See Also:
ExecutorService.submit(Runnable)
-
queue
<V> Future<V> queue(String id, Callable<V> code)
Queues the given code for later execution in a dispatch thread associated with the specified ID, returning immediately.- Parameters:
id- The ID designating which dispatch thread will execute the code.code- The code to execute.- Returns:
- A
FuturewhoseFuture.get()method blocks until the queued code has completed executing and returns the result of the execution. - See Also:
ExecutorService.submit(Callable)
-
getParent
Thread getParent(Thread thread)
Returns the thread that called the specified thread.This works only on threads which the thread service knows about, of course.
- Parameters:
thread- the managed thread, null refers to the current thread- Returns:
- the thread that asked the
ThreadServiceto spawn the specified thread
-
getThreadContext
ThreadService.ThreadContext getThreadContext(Thread thread)
Analyzes theContextof the given thread.- Parameters:
thread- The thread to analyze.- Returns:
- Information about the thread's
Context. Either:ThreadService.ThreadContext.SAME- The thread was spawned by this very thread service, and thus shares the sameContext.ThreadService.ThreadContext.OTHER- The thread was spawned by a different thread service, and thus has a differentContext.ThreadService.ThreadContext.NONE- It is unknown what spawned the thread, so it is effectivelyContext-free.
-
-