The Executor (thread pool)

Executor (thread pool)

The Executor represents a thread pool that can be shared between components in Tomcat. Historically there has been a thread pool per connector created but this allows you to share a thread pool, between (primarly) connector but also other components when those get configured to support executors
The executor has to implement the
org.apache.catalina.Executor
The executor is a nested element to the Service element. And in order for it to be picked up by the connectors, the Executor element has to appear prior to the Connector element in server.xml
All implementations of Executor support the following attributes:

Attributes

Common Attributes

All implementations of Executor support the following attributes:

Attribute Description

className : The class of the implementation. The implementation has to implement theorg.apache.catalina.Executor interface. This interface ensures that the object can be referenced through its name attribute and that implements Lifecycle, so that it can be started and stopped with the container. The default value for the className is org.apache.catalina.core.StandardThreadExecutor
name : The name used to reference this pool in other places in server.xml. The name is required and must be unique.

Standard Implementation

The default implementation supports the following attributes:

Attribute Description

threadPriority (int) : The thread priority for threads in the executor, the default is Thread.NORM_PRIORITY
daemon (boolean) : Whether the threads should be daemon threads or not, the default is true
namePrefix (String) : The name prefix for each thread created by the executor. The thread name for an individual thread will be namePrefix+threadNumber
maxThreads (int) : The max number of active threads in this pool, default is 200
minSpareThreads (int) : The minimum number of threads always kept alive, default is 25
maxIdleTime (int) : The number of milliseconds before an idle thread shutsdown, unless the number of active threads are less or equal to minSpareThreads. Default value is 60000(1 minute)

Example :

server.xml looks like the following:
<!–The connectors can use a shared executor, you can define one or more named thread pools–>

<Executor name=”tomcatThreadPool” namePrefix=”catalina-exec-” maxThreads=”200″ minSpareThreads=”4″/>

<Connector executor=”tomcatThreadPool” port=”8080″ protocol=”HTTP/1.1″ connectionTimeout=”10000″ maxKeepAliveRequests=”1″ redirectPort=”8443″ />

<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ />
However, in the Tomcat manager (http://localhost/manager/status) it shows to following

http-8080: Max threads: -1 Current thread count: -1 Current thread busy:-1
jk-8009: Max threads: 200 Current thread count: 4 Current thread busy: 1

0 threads will be created after the tomcat startup, depending on the number of concurrent requests number of threads will be created in the thread pool, for instance if there are 2 concurrent requests then 2 threads will be created in the pool and these threads are further reused by subsequent requests until more concurrent requests come where further threads will be created. Because of the minSpareThreads value of 4 all the threads created up to 4 will not be discarded irrespective of whether the threads are idle or not.

As shown in standard implementation table Thread pools shrink based on the maxIdleTime which by default is 60000 ms/ 60 secs / 1 minute meaning if a thread is idle for 60 secs tomcat will discard it from the pool.

0 comments:

Post a Comment