Package com.lucidworks.apollo.component
Class DefaultExecutorComponent
java.lang.Object
com.lucidworks.apollo.component.DefaultExecutorComponent
- All Implemented Interfaces:
ExecutorComponent,Closeable,AutoCloseable
A convenience class for creating executor services. It handles the boilerplate of custom thread names (great for
stack traces!) and registering the executor services centrally so that they can be shut down appropriately.
Provides a potential point for instrumentation, too, should we want to.
Every method in this class returns a ListeningExecutorService, so that you
can listen for task completion if you would like.
If you're looking for scheduled thread pools, use ScheduledRunnableComponent instead. It handles more of that for you.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classJust like the java.util.concurrent.ThreadPoolExecutor.AbortPolicy, but with a better error message.class -
Constructor Summary
ConstructorsConstructorDescriptionDefaultExecutorComponent(com.codahale.metrics.MetricRegistry metricRegistry) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()com.google.common.util.concurrent.ListeningExecutorServicegetOrCreateCachedThreadPool(String threadNameFormat) com.google.common.util.concurrent.ListeningExecutorServicegetOrCreateFixedThreadPool(int nThreads, String threadNameFormat) com.google.common.util.concurrent.ListeningExecutorServicegetOrCreateSingleThreadExecutor(String threadPoolName, String threadNameFormat) TODO: experimental.com.google.common.util.concurrent.ListeningExecutorServicenewCachedThreadPool(String threadNameFormat) Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.com.google.common.util.concurrent.ListeningExecutorServicenewFixedThreadPool(int nThreads, String threadNameFormat) Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue, using the provided ThreadFactory to create new threads when needed.com.google.common.util.concurrent.ListeningExecutorServicenewSingleThreadExecutor(String threadNameFormat)
-
Constructor Details
-
DefaultExecutorComponent
public DefaultExecutorComponent(com.codahale.metrics.MetricRegistry metricRegistry)
-
-
Method Details
-
getOrCreateSingleThreadExecutor
public com.google.common.util.concurrent.ListeningExecutorService getOrCreateSingleThreadExecutor(String threadPoolName, String threadNameFormat) TODO: experimental. Not sure if we want this kind of interface. If you want to reuse a thread pool between multiple components, you can use this.- Specified by:
getOrCreateSingleThreadExecutorin interfaceExecutorComponent- Parameters:
threadPoolName- the name of the thread pool to get. Multiple requests with the same thread pool name will return the same thread pool.threadNameFormat- aString.format(String, Object...)-compatible format String, to which a unique integer (0, 1, etc.) will be supplied as the single parameter. This integer will be unique to the built instance of the ThreadFactory and will be assigned sequentially. For example,"rpc-pool-%d"will generate thread names like"rpc-pool-0","rpc-pool-1","rpc-pool-2", etc.- Returns:
-
getOrCreateFixedThreadPool
public com.google.common.util.concurrent.ListeningExecutorService getOrCreateFixedThreadPool(int nThreads, String threadNameFormat) - Specified by:
getOrCreateFixedThreadPoolin interfaceExecutorComponent
-
newSingleThreadExecutor
public com.google.common.util.concurrent.ListeningExecutorService newSingleThreadExecutor(String threadNameFormat) - Specified by:
newSingleThreadExecutorin interfaceExecutorComponent- Parameters:
threadNameFormat- aString.format(String, Object...)-compatible format String, to which a unique integer (0, 1, etc.) will be supplied as the single parameter. This integer will be unique to the built instance of the ThreadFactory and will be assigned sequentially. For example,"rpc-pool-%d"will generate thread names like"rpc-pool-0","rpc-pool-1","rpc-pool-2", etc.- Returns:
- the newly created thread pool
-
newFixedThreadPool
public com.google.common.util.concurrent.ListeningExecutorService newFixedThreadPool(int nThreads, String threadNameFormat) Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue, using the provided ThreadFactory to create new threads when needed. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitlyshutdown.- Specified by:
newFixedThreadPoolin interfaceExecutorComponent- Parameters:
nThreads- the number of threads in the poolthreadNameFormat- aString.format(String, Object...)-compatible format String, to which a unique integer (0, 1, etc.) will be supplied as the single parameter. This integer will be unique to the built instance of the ThreadFactory and will be assigned sequentially. For example,"rpc-pool-%d"will generate thread names like"rpc-pool-0","rpc-pool-1","rpc-pool-2", etc.- Returns:
- the newly created thread pool
-
newCachedThreadPool
public com.google.common.util.concurrent.ListeningExecutorService newCachedThreadPool(String threadNameFormat) Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created usingThreadPoolExecutorconstructors. Note that unlike java.util.concurrent.Executors#newCachedThreadPool, this implementation does limit the pool size to 15,000 threads, since that is a practical limit on a Linux machine with ~16gb of RAM. That is based on the assumptions that: 1.) if you hit that limit, something is going wrong in your system, and 2.) it's at least marginally better to fail at the point of execution with a RejectedExecutionException versus an OutOfMemoryError with a "could not create new native thread" message. Both are probably terminal, but at least the former may be slightly more understandable?- Specified by:
newCachedThreadPoolin interfaceExecutorComponent- Parameters:
threadNameFormat- aString.format(String, Object...)-compatible format String, to which a unique integer (0, 1, etc.) will be supplied as the single parameter. This integer will be unique to the built instance of the ThreadFactory and will be assigned sequentially. For example,"rpc-pool-%d"will generate thread names like"rpc-pool-0","rpc-pool-1","rpc-pool-2", etc.- Returns:
- the newly created thread pool
-
getOrCreateCachedThreadPool
public com.google.common.util.concurrent.ListeningExecutorService getOrCreateCachedThreadPool(String threadNameFormat) - Specified by:
getOrCreateCachedThreadPoolin interfaceExecutorComponent
-
newBuilder
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-