public class SingleThreadExecutor extends java.lang.Object implements java.lang.Runnable, ExecutorService
Constructor and Description |
---|
SingleThreadExecutor() |
Modifier and Type | Method and Description |
---|---|
boolean |
awaitTermination(long timeout,
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(java.lang.Runnable r)
Executes the given command at some time in the future.
|
<T> java.util.List<Future<T>> |
invokeAll(java.util.Collection<? extends Callable<T>> tasks)
Executes the given tasks, returning a list of Futures holding their status and results when all complete.
|
<T> java.util.List<Future<T>> |
invokeAll(java.util.Collection<? extends Callable<T>> tasks,
long timeout,
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.
|
<T> T |
invokeAny(java.util.Collection<? extends Callable<T>> tasks)
Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an
exception), if any do.
|
<T> T |
invokeAny(java.util.Collection<? extends Callable<T>> tasks,
long timeout,
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 |
isShutdown()
Returns true if this executor has been shut down.
|
boolean |
isTerminated()
Returns true if all tasks have completed following shut down.
|
void |
run() |
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.
|
<T> Future<T> |
submit(Callable<T> task)
Submits a value-returning task for execution and returns a Future representing the pending results of the task.
|
Future<?> |
submit(java.lang.Runnable task)
Submits a Runnable task for execution and returns a Future representing that task.
|
<T> Future<T> |
submit(java.lang.Runnable task,
T result)
Submits a Runnable task for execution and returns a Future representing that task.
|
public void run()
run
in interface java.lang.Runnable
public void execute(java.lang.Runnable r)
Executor
public void shutdown()
ExecutorService
This method does not wait for previously submitted tasks to complete execution. Use awaitTermination
to do that.
shutdown
in interface ExecutorService
public java.util.List<java.lang.Runnable> shutdownNow()
ExecutorService
This method does not wait for actively executing tasks to terminate. Use awaitTermination
to do that.
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.
shutdownNow
in interface ExecutorService
public boolean isShutdown()
ExecutorService
isShutdown
in interface ExecutorService
public boolean isTerminated()
ExecutorService
isTerminated
in interface ExecutorService
public boolean awaitTermination(long timeout, TimeUnit unit) throws java.lang.InterruptedException
ExecutorService
awaitTermination
in interface ExecutorService
timeout
- the maximum time to waitunit
- the time unit of the timeout argumentjava.lang.InterruptedException
- if interrupted while waitingpublic <T> Future<T> submit(Callable<T> task)
ExecutorService
If you would like to immediately block waiting for a task, you can use constructions of the form result = exec.submit(aCallable).get();
submit
in interface ExecutorService
task
- the task to submitpublic <T> Future<T> submit(java.lang.Runnable task, T result)
ExecutorService
submit
in interface ExecutorService
task
- the task to submitresult
- the result to returnpublic Future<?> submit(java.lang.Runnable task)
ExecutorService
submit
in interface ExecutorService
task
- the task to submitpublic <T> java.util.List<Future<T>> invokeAll(java.util.Collection<? extends Callable<T>> tasks) throws java.lang.InterruptedException
ExecutorService
Future.isDone()
is true for each element of the returned list. Note that a completed task
could have terminated either normally or by throwing an exception. The results of this method are undefined if
the given collection is modified while this operation is in progress.invokeAll
in interface ExecutorService
tasks
- the collection of tasksjava.lang.InterruptedException
- if interrupted while waiting, in which case unfinished tasks are cancelled.public <T> java.util.List<Future<T>> invokeAll(java.util.Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws java.lang.InterruptedException
ExecutorService
Future.isDone()
is true for each element of the returned
list. Upon return, tasks that have not completed are cancelled. Note that a completed task could have
terminated either normally or by throwing an exception. The results of this method are undefined if the given
collection is modified while this operation is in progress.invokeAll
in interface ExecutorService
tasks
- the collection of taskstimeout
- the maximum time to waitunit
- the time unit of the timeout argumentjava.lang.InterruptedException
- if interrupted while waiting, in which case unfinished tasks are cancelledpublic <T> T invokeAny(java.util.Collection<? extends Callable<T>> tasks) throws java.lang.InterruptedException, ExecutionException
ExecutorService
invokeAny
in interface ExecutorService
tasks
- the collection of tasksjava.lang.InterruptedException
- if interrupted while waitingExecutionException
- if no task successfully completespublic <T> T invokeAny(java.util.Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws java.lang.InterruptedException, ExecutionException, TimeoutException
ExecutorService
invokeAny
in interface ExecutorService
tasks
- the collection of taskstimeout
- the maximum time to waitunit
- the time unit of the timeout argumentjava.lang.InterruptedException
- if interrupted while waitingExecutionException
- if no task successfully completesTimeoutException
- if the given timeout elapses before any task successfully completes