org.scijava.thread
Class DefaultThreadService

java.lang.Object
  extended by org.scijava.AbstractContextual
      extended by org.scijava.plugin.AbstractRichPlugin
          extended by org.scijava.service.AbstractService
              extended by org.scijava.thread.DefaultThreadService
All Implemented Interfaces:
Comparable<Prioritized>, ThreadFactory, Contextual, Disposable, HasPluginInfo, RichPlugin, SciJavaPlugin, Prioritized, SciJavaService, Service, ThreadService

public final class DefaultThreadService
extends AbstractService
implements ThreadService

Default service for managing active threads.

Author:
Curtis Rueden

Constructor Summary
DefaultThreadService()
           
 
Method Summary
 void dispose()
          Performs any needed cleanup of the object's services, in preparation for the object being retired (e.g., to make garbage collection possible).
 void invoke(Runnable code)
          Executes the given code in a special dispatch thread, blocking until execution is complete.
 boolean isDispatchThread()
          Gets whether the current thread is a dispatch thread for use with ThreadService.invoke(java.lang.Runnable) and ThreadService.queue(java.lang.Runnable).
 Thread newThread(Runnable r)
           
 void queue(Runnable code)
          Queues the given code for later execution in a special dispatch thread.
<V> Future<V>
run(Callable<V> code)
          Asynchronously executes the given code in a new thread, as decided by the thread service.
 Future<?> run(Runnable code)
          Asynchronously executes the given code in a new thread, as decided by the thread service.
 
Methods inherited from class org.scijava.service.AbstractService
getContext, initialize, registerEventHandlers, setContext, toString
 
Methods inherited from class org.scijava.plugin.AbstractRichPlugin
compareTo, 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.service.Service
initialize, registerEventHandlers
 
Methods inherited from interface org.scijava.Contextual
context, getContext, setContext
 
Methods inherited from interface org.scijava.Prioritized
getPriority, setPriority
 
Methods inherited from interface java.lang.Comparable
compareTo
 
Methods inherited from interface org.scijava.plugin.HasPluginInfo
getInfo, setInfo
 

Constructor Detail

DefaultThreadService

public DefaultThreadService()
Method Detail

run

public <V> Future<V> run(Callable<V> code)
Description copied from interface: ThreadService
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.

Specified by:
run in interface ThreadService
Parameters:
code - The code to execute.
Returns:
A Future that will contain the result once the execution has finished. Call Future.get() to access to the return value (which will block until execution has completed).

run

public Future<?> run(Runnable code)
Description copied from interface: ThreadService
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.

Specified by:
run in interface ThreadService
Parameters:
code - The code to execute.
Returns:
A Future that can be used to block until the execution has finished. Call Future.get() to do so.

isDispatchThread

public boolean isDispatchThread()
Description copied from interface: ThreadService
Gets whether the current thread is a dispatch thread for use with ThreadService.invoke(java.lang.Runnable) and ThreadService.queue(java.lang.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:
isDispatchThread in interface ThreadService
Returns:
True iff the current thread is considered a dispatch thread.

invoke

public void invoke(Runnable code)
            throws InterruptedException,
                   InvocationTargetException
Description copied from interface: ThreadService
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.

Specified by:
invoke in interface ThreadService
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: ThreadService
Queues the given code for later execution in a special dispatch thread.

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:
queue in interface ThreadService
Parameters:
code - The code to execute.

dispose

public void dispose()
Description copied from interface: Disposable
Performs any needed cleanup of the object's services, in preparation for the object being retired (e.g., to make garbage collection possible).

Specified by:
dispose in interface Disposable
Overrides:
dispose in class AbstractService

newThread

public Thread newThread(Runnable r)
Specified by:
newThread in interface ThreadFactory


Copyright © 2009–2014 SciJava. All rights reserved.