public class SingleThreadExecutor extends Object implements 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(Runnable r)
Executes the given command at some time in the future.
|
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks)
Executes the given tasks, returning a list of Futures holding their status and results when all complete.
|
<T> List<Future<T>> |
invokeAll(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(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(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()
When an object implementing interface
Runnable is used to create a thread, starting
the thread causes the object's run method to be called in that separately executing
thread. |
void |
shutdown()
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be
accepted.
|
List<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(Runnable task)
Submits a Runnable task for execution and returns a Future representing that task.
|
<T> Future<T> |
submit(Runnable task,
T result)
Submits a Runnable task for execution and returns a Future representing that task.
|
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
ExecutorService
awaitTermination
in interface ExecutorService
timeout
- the maximum time to waitunit
- the time unit of the timeout argumentInterruptedException
- if interrupted while waitingpublic void execute(Runnable r)
Executor
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws 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 tasksInterruptedException
- if interrupted while waiting, in which case unfinished tasks are cancelled.public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws 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 argumentInterruptedException
- if interrupted while waiting, in which case unfinished tasks are cancelledpublic <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
ExecutorService
invokeAny
in interface ExecutorService
tasks
- the collection of tasksInterruptedException
- if interrupted while waitingExecutionException
- if no task successfully completespublic <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
ExecutorService
invokeAny
in interface ExecutorService
tasks
- the collection of taskstimeout
- the maximum time to waitunit
- the time unit of the timeout argumentInterruptedException
- if interrupted while waitingExecutionException
- if no task successfully completesTimeoutException
- if the given timeout elapses before any task successfully completespublic boolean isShutdown()
ExecutorService
isShutdown
in interface ExecutorService
public boolean isTerminated()
ExecutorService
isTerminated
in interface ExecutorService
public void run()
Runnable
Runnable
is used to create a thread, starting
the thread causes the object's run
method to be called in that separately executing
thread.
The general contract of the method run
is that it may take any action whatsoever.
run
in interface Runnable
Thread.run()
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 List<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 <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 Future<?> submit(Runnable task)
ExecutorService
submit
in interface ExecutorService
task
- the task to submitpublic <T> Future<T> submit(Runnable task, T result)
ExecutorService
submit
in interface ExecutorService
task
- the task to submitresult
- the result to return