Package org.jdesktop.application
Class TaskService
- java.lang.Object
-
- org.jdesktop.application.AbstractBean
-
- org.jdesktop.application.TaskService
-
public class TaskService extends AbstractBean
The service for executing tasksMethods descriptions are copied from
ExecutorService
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_NAME
-
Constructor Summary
Constructors Constructor Description TaskService(java.lang.String name)
Creates a newTaskService
with default executor.TaskService(java.lang.String name, java.util.concurrent.ExecutorService executorService)
Creates a newTaskService
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
awaitTermination(long timeout, java.util.concurrent.TimeUnit unit)
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.void
execute(Task task)
Executes the task.java.lang.String
getName()
Gets the name of this task servicejava.util.List<Task>
getTasks()
Returns the list of tasks which are executing by this serviceboolean
isShutdown()
Returns true if this executor has been shut down.boolean
isTerminated()
Returns true if all tasks have completed following shut down.void
shutdown()
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.java.util.List<java.lang.Runnable>
shutdownNow()
Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.-
Methods inherited from class org.jdesktop.application.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, firePropertyChange, firePropertyChange, getPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
-
-
-
-
Field Detail
-
DEFAULT_NAME
public static final java.lang.String DEFAULT_NAME
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TaskService
public TaskService(java.lang.String name, java.util.concurrent.ExecutorService executorService)
Creates a newTaskService
- Parameters:
name
- the name of the task serviceexecutorService
- the executor to be used to run tasks.
-
TaskService
public TaskService(java.lang.String name)
Creates a newTaskService
with default executor. The default executor is a ThreadPoolExecutor with core pool size = 3, maximum pool size = 10, threads live time = 1 second and queue of typeLinkedBlockingQueue
.
-
-
Method Detail
-
getName
public final java.lang.String getName()
Gets the name of this task service- Returns:
- this task service's name
-
execute
public void execute(Task task)
Executes the task.- Parameters:
task
- the task to be executed
-
getTasks
public java.util.List<Task> getTasks()
Returns the list of tasks which are executing by this service- Returns:
- the list of tasks which are executing by this service
-
shutdown
public final void shutdown()
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.- Throws:
java.lang.SecurityException
- if a security manager exists and shutting down this ExecutorService may manipulate threads that the caller is not permitted to modify because it does not holdRuntimePermission
("modifyThread"), or the security manager's checkAccess method denies access.
-
shutdownNow
public final java.util.List<java.lang.Runnable> shutdownNow()
Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via
Thread.interrupt()
, so any task that fails to respond to interrupts may never terminate.- Returns:
- list of tasks that never commenced execution
- Throws:
java.lang.SecurityException
- if a security manager exists and shutting down this ExecutorService may manipulate threads that the caller is not permitted to modify because it does not holdRuntimePermission
("modifyThread"), or the security manager's checkAccess method denies access.
-
isShutdown
public final boolean isShutdown()
Returns true if this executor has been shut down.- Returns:
- true if this executor has been shut down
-
isTerminated
public final boolean isTerminated()
Returns true if all tasks have completed following shut down. Note that isTerminated is never true unless either shutdown or shutdownNow was called first.- Returns:
- true if all tasks have completed following shut down
-
awaitTermination
public final boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.- Parameters:
timeout
- the maximum time to waitunit
- the time unit of the timeout argument- Returns:
- true if this executor terminated and false if the timeout elapsed before termination
- Throws:
java.lang.InterruptedException
- if interrupted while waiting
-
-