public class TaskService extends Service implements java.util.concurrent.ScheduledExecutorService
Application.getCurrent()
still work.wrap(ScheduledExecutorService)
method to ensure that thread local variables are correctly set.Constructor and Description |
---|
TaskService()
Constructor.
|
TaskService(boolean enabled)
Constructor.
|
TaskService(boolean enabled,
boolean daemon)
Constructor.
|
TaskService(boolean enabled,
int corePoolSize)
Constructor.
|
TaskService(int corePoolSize)
Constructor.
|
Modifier and Type | Method and 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.
|
protected java.util.concurrent.ScheduledExecutorService |
createExecutorService(int corePoolSize)
Creates a new JDK executor service that will be wrapped.
|
protected java.util.concurrent.ThreadFactory |
createThreadFactory()
Creates a new thread factory that will properly name the Restlet created
threads with a "restlet-" prefix.
|
void |
execute(java.lang.Runnable command)
Executes the given command asynchronously.
|
int |
getCorePoolSize()
Returns the core pool size defining the maximum number of threads.
|
java.util.List |
invokeAll(java.util.Collection tasks)
Executes the given tasks, returning a list of Futures holding their
status and results when all complete.
|
java.util.List |
invokeAll(java.util.Collection tasks,
long timeout,
java.util.concurrent.TimeUnit unit)
Executes the given tasks, returning a list of Futures holding their
status and results when all complete or the timeout expires, whichever
happens first.
|
java.lang.Object |
invokeAny(java.util.Collection tasks)
Executes the given tasks, returning the result of one that has completed
successfully (i.e., without throwing an exception), if any do.
|
java.lang.Object |
invokeAny(java.util.Collection tasks,
long timeout,
java.util.concurrent.TimeUnit unit)
Executes the given tasks, returning the result of one that has completed
successfully (i.e., without throwing an exception), if any do before the
given timeout elapses.
|
boolean |
isDaemon()
Indicates whether the threads are created as daemon threads.
|
boolean |
isShutdown()
Returns true if this executor has been shut down.
|
boolean |
isShutdownAllowed()
Indicates if the
shutdown() and shutdownNow() methods
are allowed to effectively shutdown the wrapped executor service. |
boolean |
isTerminated()
Returns true if all tasks have completed following shut down.
|
<V> java.util.concurrent.ScheduledFuture<V> |
schedule(java.util.concurrent.Callable<V> callable,
long delay,
java.util.concurrent.TimeUnit unit)
Creates and executes a ScheduledFuture that becomes enabled after the
given delay.
|
java.util.concurrent.ScheduledFuture<?> |
schedule(java.lang.Runnable command,
long delay,
java.util.concurrent.TimeUnit unit)
Creates and executes a one-shot action that becomes enabled after the
given delay.
|
java.util.concurrent.ScheduledFuture<?> |
scheduleAtFixedRate(java.lang.Runnable command,
long initialDelay,
long period,
java.util.concurrent.TimeUnit unit)
Creates and executes a periodic action that becomes enabled first after
the given initial delay, and subsequently with the given period; that is
executions will commence after initialDelay then
initialDelay+period, then initialDelay + 2 * period,
and so on.
|
java.util.concurrent.ScheduledFuture<?> |
scheduleWithFixedDelay(java.lang.Runnable command,
long initialDelay,
long delay,
java.util.concurrent.TimeUnit unit)
Creates and executes a periodic action that becomes enabled first after
the given initial delay, and subsequently with the given delay between
the termination of one execution and the commencement of the next.
|
void |
setCorePoolSize(int corePoolSize)
Sets the core pool size defining the maximum number of threads.
|
void |
setDaemon(boolean daemon)
Indicates whether or not the threads are daemon threads.
|
void |
setShutdownAllowed(boolean allowShutdown)
Indicates if the
shutdown() and shutdownNow() methods
are allowed to effectively shutdown the wrapped executor service. |
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.
|
void |
start()
Starts the Restlet.
|
void |
stop()
Stops the Restlet.
|
<T> java.util.concurrent.Future<T> |
submit(java.util.concurrent.Callable<T> task)
Submits a value-returning task for execution and returns a Future
representing the pending results of the task.
|
java.util.concurrent.Future<?> |
submit(java.lang.Runnable task) |
<T> java.util.concurrent.Future<T> |
submit(java.lang.Runnable task,
T result) |
static java.util.concurrent.ScheduledExecutorService |
wrap(java.util.concurrent.ScheduledExecutorService executorService)
Wraps a JDK executor service to ensure that the threads executing the
tasks will have the thread local variables copied from the calling
thread.
|
createInboundFilter, createOutboundFilter, getContext, isEnabled, isStarted, isStopped, setContext, setEnabled
public TaskService()
public TaskService(boolean enabled)
enabled
- True if the service has been enabled.public TaskService(boolean enabled, boolean daemon)
enabled
- True if the service has been enabled.daemon
- True if the threads are created as daemon threads.public TaskService(boolean enabled, int corePoolSize)
enabled
- True if the service has been enabled.corePoolSize
- The core pool size defining the maximum number of threads.public TaskService(int corePoolSize)
corePoolSize
- The core pool size defining the maximum number of threads.public static java.util.concurrent.ScheduledExecutorService wrap(java.util.concurrent.ScheduledExecutorService executorService)
Application.getCurrent()
still work.executorService
- The JDK service to wrap.public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
awaitTermination
in interface java.util.concurrent.ExecutorService
timeout
- The maximum time to wait.unit
- The time unit.java.lang.InterruptedException
protected java.util.concurrent.ScheduledExecutorService createExecutorService(int corePoolSize)
Executors.newCachedThreadPool(ThreadFactory)
, passing the
result of createThreadFactory()
as a parameter.corePoolSize
- The core pool size defining the maximum number of threads.protected java.util.concurrent.ThreadFactory createThreadFactory()
public void execute(java.lang.Runnable command)
execute
in interface java.util.concurrent.Executor
command
- The command to execute.public int getCorePoolSize()
public java.util.List invokeAll(java.util.Collection tasks) throws java.lang.InterruptedException
ExecutorService
interface for typing details.invokeAll
in interface java.util.concurrent.ExecutorService
tasks
- The task to execute.java.lang.InterruptedException
public java.util.List invokeAll(java.util.Collection tasks, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
ExecutorService
interface for typing details.invokeAll
in interface java.util.concurrent.ExecutorService
tasks
- The task to execute.timeout
- The maximum time to wait.unit
- The time unit.java.lang.InterruptedException
public java.lang.Object invokeAny(java.util.Collection tasks) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
ExecutorService
interface for typing details.invokeAny
in interface java.util.concurrent.ExecutorService
tasks
- The task to execute.java.lang.InterruptedException
java.util.concurrent.ExecutionException
public java.lang.Object invokeAny(java.util.Collection tasks, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
ExecutorService
interface for typing details.invokeAny
in interface java.util.concurrent.ExecutorService
tasks
- The task to execute.timeout
- The maximum time to wait.unit
- The time unit.java.lang.InterruptedException
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException
public boolean isDaemon()
public boolean isShutdown()
isShutdown
in interface java.util.concurrent.ExecutorService
public boolean isShutdownAllowed()
shutdown()
and shutdownNow()
methods
are allowed to effectively shutdown the wrapped executor service. Return
false by default.public boolean isTerminated()
isTerminated
in interface java.util.concurrent.ExecutorService
public <V> java.util.concurrent.ScheduledFuture<V> schedule(java.util.concurrent.Callable<V> callable, long delay, java.util.concurrent.TimeUnit unit)
schedule
in interface java.util.concurrent.ScheduledExecutorService
callable
- The function to execute.delay
- The time from now to delay execution.unit
- The time unit of the delay parameter.java.util.concurrent.RejectedExecutionException
- if task cannot be scheduled for execution.java.lang.NullPointerException
- if callable is nullpublic java.util.concurrent.ScheduledFuture<?> schedule(java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit)
schedule
in interface java.util.concurrent.ScheduledExecutorService
command
- The task to execute.delay
- The time from now to delay execution.unit
- The time unit of the delay parameter.java.util.concurrent.RejectedExecutionException
- if task cannot be scheduled for execution.java.lang.NullPointerException
- if command is nullpublic java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate(java.lang.Runnable command, long initialDelay, long period, java.util.concurrent.TimeUnit unit)
scheduleAtFixedRate
in interface java.util.concurrent.ScheduledExecutorService
command
- The task to execute.initialDelay
- The time to delay first execution.period
- The period between successive executions.unit
- The time unit of the initialDelay and period parametersjava.util.concurrent.RejectedExecutionException
- if task cannot be scheduled for execution.java.lang.NullPointerException
- if command is nulljava.lang.IllegalArgumentException
- if period less than or equal to zero.public java.util.concurrent.ScheduledFuture<?> scheduleWithFixedDelay(java.lang.Runnable command, long initialDelay, long delay, java.util.concurrent.TimeUnit unit)
scheduleWithFixedDelay
in interface java.util.concurrent.ScheduledExecutorService
command
- The task to execute.initialDelay
- The time to delay first execution.delay
- The delay between the termination of one execution and the
commencement of the next.unit
- The time unit of the initialDelay and delay parametersjava.util.concurrent.RejectedExecutionException
- if task cannot be scheduled for execution.java.lang.NullPointerException
- if command is nulljava.lang.IllegalArgumentException
- if delay less than or equal to zero.public void setCorePoolSize(int corePoolSize)
corePoolSize
- The core pool size defining the maximum number of threads.public void setDaemon(boolean daemon)
daemon
- True if the threads are daemon threads.public void setShutdownAllowed(boolean allowShutdown)
shutdown()
and shutdownNow()
methods
are allowed to effectively shutdown the wrapped executor service.allowShutdown
- True if shutdown is allowed.public void shutdown()
shutdown
in interface java.util.concurrent.ExecutorService
public java.util.List<java.lang.Runnable> shutdownNow()
shutdownNow
in interface java.util.concurrent.ExecutorService
public void start() throws java.lang.Exception
Service
public void stop() throws java.lang.Exception
Service
public <T> java.util.concurrent.Future<T> submit(java.util.concurrent.Callable<T> task)
submit
in interface java.util.concurrent.ExecutorService
task
- The task to submit.public java.util.concurrent.Future<?> submit(java.lang.Runnable task)
submit
in interface java.util.concurrent.ExecutorService
task
- The task to submit.public <T> java.util.concurrent.Future<T> submit(java.lang.Runnable task, T result)
submit
in interface java.util.concurrent.ExecutorService
task
- The task to submit.result
- The result to return.Copyright © 2005-2024 Restlet.