Class SingleThreadExecutor
- java.lang.Object
-
- ej.util.concurrent.SingleThreadExecutor
-
- All Implemented Interfaces:
Runnable,Executor,ExecutorService
public class SingleThreadExecutor extends Object implements Runnable, ExecutorService
An executor that uses a single worker thread operating off an unbounded queue.
-
-
Constructor Summary
Constructors Constructor Description SingleThreadExecutor()Creates a single thread executor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanawaitTermination(long timeout, TimeUnit unit)This method is unsupported by this implementation.voidexecute(Runnable r)Executes the given command at some time in the future.<T> List<Future<T>>invokeAll(Collection<? extends Callable<T>> tasks)This method is unsupported by this implementation.<T> List<Future<T>>invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)This method is unsupported by this implementation.<T> TinvokeAny(Collection<? extends Callable<T>> tasks)This method is unsupported by this implementation.<T> TinvokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)This method is unsupported by this implementation.booleanisShutdown()Returns true if this executor has been shut down.booleanisTerminated()Returns true if all tasks have completed following shut down.voidrun()When an object implementing interfaceRunnableis used to create a thread, starting the thread causes the object'srunmethod to be called in that separately executing thread.voidshutdown()Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.List<Runnable>shutdownNow()This method is unsupported by this implementation.Future<?>submit(Runnable task)This method is unsupported by this implementation.<T> Future<T>submit(Runnable task, T result)This method is unsupported by this implementation.<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.
-
-
-
Method Detail
-
run
public void run()
Description copied from interface:RunnableWhen an object implementing interfaceRunnableis used to create a thread, starting the thread causes the object'srunmethod to be called in that separately executing thread.The general contract of the method
runis that it may take any action whatsoever.- Specified by:
runin interfaceRunnable- See Also:
Thread.run()
-
execute
public void execute(Runnable r)
Description copied from interface:ExecutorExecutes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.
-
shutdown
public void shutdown()
Description copied from interface:ExecutorServiceInitiates 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.This method does not wait for previously submitted tasks to complete execution. Use
awaitTerminationto do that.- Specified by:
shutdownin interfaceExecutorService
-
shutdownNow
public List<Runnable> shutdownNow()
This method is unsupported by this implementation.Calling this method will throw an
UnsupportedOperationException.- Specified by:
shutdownNowin interfaceExecutorService- Returns:
- list of tasks that never commenced execution
-
isShutdown
public boolean isShutdown()
Description copied from interface:ExecutorServiceReturns true if this executor has been shut down.- Specified by:
isShutdownin interfaceExecutorService- Returns:
- true if this executor has been shut down
-
isTerminated
public boolean isTerminated()
Description copied from interface:ExecutorServiceReturns true if all tasks have completed following shut down. Note that isTerminated is never true unless either shutdown or shutdownNow was called first.- Specified by:
isTerminatedin interfaceExecutorService- Returns:
- true if all tasks have completed following shut down
-
awaitTermination
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedExceptionThis method is unsupported by this implementation.Calling this method will throw an
UnsupportedOperationException.- Specified by:
awaitTerminationin interfaceExecutorService- 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:
InterruptedException- if interrupted while waiting
-
submit
public <T> Future<T> submit(Callable<T> task)
Description copied from interface:ExecutorServiceSubmits a value-returning task for execution and returns a Future representing the pending results of the task. The Future's get method will return the task's result upon successful completion.If you would like to immediately block waiting for a task, you can use constructions of the form result = exec.submit(aCallable).get();
- Specified by:
submitin interfaceExecutorService- Parameters:
task- the task to submit- Returns:
- a Future representing pending completion of the task
-
submit
public <T> Future<T> submit(Runnable task, T result)
This method is unsupported by this implementation.Calling this method will throw an
UnsupportedOperationException.- Specified by:
submitin interfaceExecutorService- Parameters:
task- the task to submitresult- the result to return- Returns:
- a Future representing pending completion of the task
-
submit
public Future<?> submit(Runnable task)
This method is unsupported by this implementation.Calling this method will throw an
UnsupportedOperationException.- Specified by:
submitin interfaceExecutorService- Parameters:
task- the task to submit- Returns:
- a Future representing pending completion of the task
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
This method is unsupported by this implementation.Calling this method will throw an
UnsupportedOperationException.- Specified by:
invokeAllin interfaceExecutorService- Parameters:
tasks- the collection of tasks- Returns:
- A list of Futures representing the tasks, in the same sequential order as produced by the iterator for the given task list, each of which has completed.
- Throws:
InterruptedException- if interrupted while waiting, in which case unfinished tasks are cancelled.
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
This method is unsupported by this implementation.Calling this method will throw an
UnsupportedOperationException.- Specified by:
invokeAllin interfaceExecutorService- Parameters:
tasks- the collection of taskstimeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- a list of Futures representing the tasks, in the same sequential order as produced by the iterator for the given task list. If the operation did not time out, each task will have completed. If it did time out, some of these tasks will not have completed.
- Throws:
InterruptedException- if interrupted while waiting, in which case unfinished tasks are cancelled
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
This method is unsupported by this implementation.Calling this method will throw an
UnsupportedOperationException.- Specified by:
invokeAnyin interfaceExecutorService- Parameters:
tasks- the collection of tasks- Returns:
- the result returned by one of the tasks
- Throws:
InterruptedException- if interrupted while waitingExecutionException- if no task successfully completes
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
This method is unsupported by this implementation.Calling this method will throw an
UnsupportedOperationException.- Specified by:
invokeAnyin interfaceExecutorService- Parameters:
tasks- the collection of taskstimeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- the result returned by one of the tasks.
- Throws:
InterruptedException- if interrupted while waitingExecutionException- if no task successfully completesTimeoutException- if the given timeout elapses before any task successfully completes
-
-