Package org.scijava.thread
Class DefaultThreadService
- java.lang.Object
-
- org.scijava.AbstractContextual
-
- org.scijava.plugin.AbstractRichPlugin
-
- org.scijava.service.AbstractService
-
- org.scijava.thread.DefaultThreadService
-
- All Implemented Interfaces:
Comparable<Prioritized>,ThreadFactory,Contextual,Disposable,Identifiable,Initializable,Locatable,Logged,HasPluginInfo,RichPlugin,SciJavaPlugin,Prioritized,SciJavaService,Service,ThreadService,Versioned
public final class DefaultThreadService extends AbstractService implements ThreadService
Default service for managing active threads.- Author:
- Curtis Rueden
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.scijava.thread.ThreadService
ThreadService.ThreadContext
-
-
Constructor Summary
Constructors Constructor Description DefaultThreadService()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddispose()Performs any needed cleanup of the object's services, in preparation for the object being retired (e.g., to make garbage collection possible).ExecutorServicegetExecutorService()Gets theExecutorServiceobject used whenThreadService.run(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 withThreadService.invoke(Runnable)andThreadService.queue(Runnable).ThreadnewThread(Runnable r)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 whenThreadService.run(java.util.concurrent.Callable<V>)is called.-
Methods inherited from class org.scijava.service.AbstractService
getContext, setContext, toString
-
Methods inherited from class org.scijava.plugin.AbstractRichPlugin
getInfo, getPriority, setInfo, setPriority
-
Methods inherited from class org.scijava.AbstractContextual
context
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.scijava.Contextual
context, getContext, setContext
-
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 org.scijava.Versioned
getVersion
-
-
-
-
Method Detail
-
run
public <V> Future<V> run(Callable<V> code)
Description copied from interface:ThreadServiceAsynchronously 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.- Specified by:
runin interfaceThreadService- 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
public Future<?> run(Runnable code)
Description copied from interface:ThreadServiceAsynchronously 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.- Specified by:
runin interfaceThreadService- 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
public ExecutorService getExecutorService()
Description copied from interface:ThreadServiceGets theExecutorServiceobject used whenThreadService.run(java.util.concurrent.Callable<V>)is called.- Specified by:
getExecutorServicein interfaceThreadService- Returns:
- the
ExecutorService, or null if anExecutorServiceis not used in thisThreadServiceimplementation.
-
setExecutorService
public void setExecutorService(ExecutorService executor)
Description copied from interface:ThreadServiceSets theExecutorServiceobject used whenThreadService.run(java.util.concurrent.Callable<V>)is called.- Specified by:
setExecutorServicein interfaceThreadService- Parameters:
executor- TheExecutorServicefor thisThreadServiceto use internally forThreadService.run(java.util.concurrent.Callable<V>)calls.
-
isDispatchThread
public boolean isDispatchThread()
Description copied from interface:ThreadServiceGets whether the current thread is a dispatch thread for use withThreadService.invoke(Runnable)andThreadService.queue(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.
- Specified by:
isDispatchThreadin interfaceThreadService- Returns:
- True iff the current thread is considered a dispatch thread.
-
invoke
public void invoke(Runnable code) throws InterruptedException, InvocationTargetException
Description copied from interface:ThreadServiceExecutes 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.
- Specified by:
invokein interfaceThreadService- 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
public void queue(Runnable code)
Description copied from interface:ThreadServiceQueues 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.
- Specified by:
queuein interfaceThreadService- Parameters:
code- The code to execute.
-
queue
public Future<?> queue(String id, Runnable code)
Description copied from interface:ThreadServiceQueues the given code for later execution in a dispatch thread associated with the specified ID, returning immediately.- Specified by:
queuein interfaceThreadService- 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
public <V> Future<V> queue(String id, Callable<V> code)
Description copied from interface:ThreadServiceQueues the given code for later execution in a dispatch thread associated with the specified ID, returning immediately.- Specified by:
queuein interfaceThreadService- 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
public Thread getParent(Thread thread)
Description copied from interface:ThreadServiceReturns the thread that called the specified thread.This works only on threads which the thread service knows about, of course.
- Specified by:
getParentin interfaceThreadService- Parameters:
thread- the managed thread, null refers to the current thread- Returns:
- the thread that asked the
ThreadServiceto spawn the specified thread
-
getThreadContext
public ThreadService.ThreadContext getThreadContext(Thread thread)
Description copied from interface:ThreadServiceAnalyzes theContextof the given thread.- Specified by:
getThreadContextin interfaceThreadService- 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.
-
dispose
public void dispose()
Description copied from interface:DisposablePerforms any needed cleanup of the object's services, in preparation for the object being retired (e.g., to make garbage collection possible).- Specified by:
disposein interfaceDisposable
-
newThread
public Thread newThread(Runnable r)
- Specified by:
newThreadin interfaceThreadFactory
-
-