Class DirectExecutorService
- All Implemented Interfaces:
AutoCloseable
,Executor
,ExecutorService
AbstractExecutorService
that executes tasks directly in the calling thread without
using any worker threads.
This class facilitates immediate execution of tasks provided to it. It does not queue tasks or execute them asynchronously. Instead, tasks are executed sequentially on the calling thread. This behavior makes it particularly useful in certain testing scenarios or for lightweight use cases where threading control is not necessary.
This class maintains an internal state to track whether the executor has been shut down, whether it is terminated, and which tasks are currently running. Once shut down, no new tasks can be executed.
Features: - Tasks are executed directly in the calling thread, without any delay or queueing. - Keeps track of
running tasks and prevents task execution after the executor has been shut down. - Supports shutting down and forced
shutdown via shutdown()
and shutdownNow()
. - Provides termination and shutdown status via
isShutdown()
and isTerminated()
. - Allows the caller to wait for termination using
awaitTermination(long, TimeUnit)
.
Thread Safety: Internally uses thread-safe mechanisms such as AtomicBoolean
and CopyOnWriteArrayList
to handle state tracking and concurrent invocations.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
awaitTermination
(long timeout, TimeUnit unit) void
boolean
boolean
static ExecutorService
void
shutdown()
Methods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.concurrent.ExecutorService
close
-
Constructor Details
-
DirectExecutorService
public DirectExecutorService()
-
-
Method Details
-
newInstance
-
execute
-
shutdown
public void shutdown() -
isShutdown
public boolean isShutdown() -
shutdownNow
-
isTerminated
public boolean isTerminated() -
awaitTermination
- Throws:
InterruptedException
-