Package org.elasticsearch.threadpool
Class ThreadPool
java.lang.Object
org.elasticsearch.threadpool.ThreadPool
- All Implemented Interfaces:
Scheduler
public class ThreadPool extends java.lang.Object implements Scheduler
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ThreadPool.Info
static class
ThreadPool.Names
static class
ThreadPool.ThreadPoolType
Nested classes/interfaces inherited from interface org.elasticsearch.threadpool.Scheduler
Scheduler.Cancellable, Scheduler.ReschedulingRunnable, Scheduler.SafeScheduledThreadPoolExecutor, Scheduler.ScheduledCancellable
-
Field Summary
Fields Modifier and Type Field Description static Setting<TimeValue>
ESTIMATED_TIME_INTERVAL_SETTING
static java.util.Map<java.lang.String,ThreadPool.ThreadPoolType>
THREAD_POOL_TYPES
-
Constructor Summary
Constructors Constructor Description ThreadPool(Settings settings, ExecutorBuilder<?>... customBuilders)
-
Method Summary
Modifier and Type Method Description long
absoluteTimeInMillis()
Returns the value of milliseconds since UNIX epoch.static boolean
assertCurrentMethodIsNotCalledRecursively()
static boolean
assertNotScheduleThread(java.lang.String reason)
boolean
awaitTermination(long timeout, java.util.concurrent.TimeUnit unit)
java.util.Collection<ExecutorBuilder>
builders()
java.util.concurrent.ExecutorService
executor(java.lang.String name)
Get theExecutorService
with the given name.java.util.concurrent.ExecutorService
generic()
Get the genericExecutorService
.ThreadContext
getThreadContext()
ThreadPoolInfo
info()
ThreadPool.Info
info(java.lang.String name)
java.lang.Runnable
preserveContext(java.lang.Runnable command)
Does nothing by default but can be used by subclasses to save the current thread context and wraps the command in a Runnable that restores that context before running the command.long
relativeTimeInMillis()
Returns a value of milliseconds that may be used for relative time calculations.long
relativeTimeInNanos()
Returns a value of nanoseconds that may be used for relative time calculations.Scheduler.ScheduledCancellable
schedule(java.lang.Runnable command, TimeValue delay, java.lang.String executor)
Schedules a one-shot command to run after a given delay.java.util.concurrent.ScheduledExecutorService
scheduler()
void
scheduleUnlessShuttingDown(TimeValue delay, java.lang.String executor, java.lang.Runnable command)
Scheduler.Cancellable
scheduleWithFixedDelay(java.lang.Runnable command, TimeValue interval, java.lang.String executor)
Schedules a periodic action that runs on scheduler thread.static int
searchThreadPoolSize(int availableProcessors)
void
shutdown()
void
shutdownNow()
ThreadPoolStats
stats()
protected void
stopCachedTimeThread()
static boolean
terminate(java.util.concurrent.ExecutorService service, long timeout, java.util.concurrent.TimeUnit timeUnit)
Returnstrue
if the given service was terminated successfully.static boolean
terminate(ThreadPool pool, long timeout, java.util.concurrent.TimeUnit timeUnit)
Returnstrue
if the given pool was terminated successfully.
-
Field Details
-
Constructor Details
-
Method Details
-
builders
-
relativeTimeInMillis
public long relativeTimeInMillis()Returns a value of milliseconds that may be used for relative time calculations. This method should only be used for calculating time deltas. For an epoch based timestamp, seeabsoluteTimeInMillis()
. -
relativeTimeInNanos
public long relativeTimeInNanos()Returns a value of nanoseconds that may be used for relative time calculations. This method should only be used for calculating time deltas. For an epoch based timestamp, seeabsoluteTimeInMillis()
. -
absoluteTimeInMillis
public long absoluteTimeInMillis()Returns the value of milliseconds since UNIX epoch. This method should only be used for exact date/time formatting. For calculating time deltas that should not suffer from negative deltas, which are possible with this method, seerelativeTimeInMillis()
. -
info
-
info
-
stats
-
generic
public java.util.concurrent.ExecutorService generic()Get the genericExecutorService
. This executor serviceExecutor.execute(Runnable)
method will run theRunnable
it is given in theThreadContext
of the thread that queues it.Warning: this ExecutorService will not throw
RejectedExecutionException
if you submit a task while it shutdown. It will instead silently queue it and not run it. -
executor
public java.util.concurrent.ExecutorService executor(java.lang.String name)Get theExecutorService
with the given name. This executor service'sExecutor.execute(Runnable)
method will run theRunnable
it is given in theThreadContext
of the thread that queues it.Warning: this ExecutorService might not throw
RejectedExecutionException
if you submit a task while it shutdown. It will instead silently queue it and not run it.- Parameters:
name
- the name of the executor service to obtain- Throws:
java.lang.IllegalArgumentException
- if no executor service with the specified name exists
-
schedule
public Scheduler.ScheduledCancellable schedule(java.lang.Runnable command, TimeValue delay, java.lang.String executor)Schedules a one-shot command to run after a given delay. The command is not run in the context of the calling thread. To preserve the context of the calling thread you may callthreadPool.getThreadContext().preserveContext
on the runnable before passing it to this method.- Specified by:
schedule
in interfaceScheduler
- Parameters:
command
- the command to rundelay
- delay before the task executesexecutor
- the name of the thread pool on which to execute this task. SAME means "execute on the scheduler thread" which changes the meaning of the ScheduledFuture returned by this method. In that case the ScheduledFuture will complete only when the command completes.- Returns:
- a ScheduledFuture who's get will return when the task is has been added to its target thread pool and throw an exception if the task is canceled before it was added to its target thread pool. Once the task has been added to its target thread pool the ScheduledFuture will cannot interact with it.
- Throws:
EsRejectedExecutionException
- if the task cannot be scheduled for execution
-
scheduleUnlessShuttingDown
public void scheduleUnlessShuttingDown(TimeValue delay, java.lang.String executor, java.lang.Runnable command) -
scheduleWithFixedDelay
public Scheduler.Cancellable scheduleWithFixedDelay(java.lang.Runnable command, TimeValue interval, java.lang.String executor)Description copied from interface:Scheduler
Schedules a periodic action that runs on scheduler thread. Do not run blocking calls on the scheduler thread. Subclasses may allow to execute on a different executor, in which case blocking calls are allowed.- Specified by:
scheduleWithFixedDelay
in interfaceScheduler
- Parameters:
command
- the action to takeinterval
- the delay intervalexecutor
- the name of the executor that has to execute this task. Ignored in the default implementation but can be used by subclasses that support multiple executors.- Returns:
- a
Scheduler.Cancellable
that can be used to cancel the subsequent runs of the command. If the command is running, it will not be interrupted.
-
preserveContext
public java.lang.Runnable preserveContext(java.lang.Runnable command)Description copied from interface:Scheduler
Does nothing by default but can be used by subclasses to save the current thread context and wraps the command in a Runnable that restores that context before running the command.- Specified by:
preserveContext
in interfaceScheduler
-
stopCachedTimeThread
protected final void stopCachedTimeThread() -
shutdown
public void shutdown() -
shutdownNow
public void shutdownNow() -
awaitTermination
public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException
-
scheduler
public java.util.concurrent.ScheduledExecutorService scheduler() -
searchThreadPoolSize
public static int searchThreadPoolSize(int availableProcessors) -
terminate
public static boolean terminate(java.util.concurrent.ExecutorService service, long timeout, java.util.concurrent.TimeUnit timeUnit)Returnstrue
if the given service was terminated successfully. If the termination timed out, the service isnull
this method will returnfalse
. -
terminate
public static boolean terminate(ThreadPool pool, long timeout, java.util.concurrent.TimeUnit timeUnit)Returnstrue
if the given pool was terminated successfully. If the termination timed out, the service isnull
this method will returnfalse
. -
getThreadContext
-
assertNotScheduleThread
public static boolean assertNotScheduleThread(java.lang.String reason) -
assertCurrentMethodIsNotCalledRecursively
public static boolean assertCurrentMethodIsNotCalledRecursively()
-