Skip to main content

Concurrency

Tomcat Threads vs ThreadPoolTaskExecutor

AspectTomcat ThreadsThreadPoolTaskExecutor
ScopeWeb server level (HTTP request handling)Application level (task execution)
Primary UseProcess incoming HTTP requestsExecute asynchronous/background tasks
ConfigurationTomcat server.xml or propertiesSpring configuration (Java/XML)
Thread ManagementManaged by TomcatManaged by Spring/application
Task TypeSynchronous HTTP request processingAsynchronous or parallel tasks
QueueingLimited queueing (depends on acceptCount)Explicit task queue with configurable size
ControlLimited application controlFull application control
Use CaseHandling client HTTP requestsOffloading long-running/non-HTTP tasks
ExampleProcessing servlet/controller requestsRunning @Async methods, batch jobs

Key Concept

  • Core Pool Size: The minimum number of threads kept alive in the pool, even when idle
  • Max Pool Size: The maximum number of threads allowed in the pool when the task queue is full
  • Queue Capacity: The number of tasks that can be queued when all core threads are busy (before scaling up to maxPoolSize)

Example: To analyze the first 200 requests that arrive to the server in just 0.02 seconds(20ms), processing time of each task: 10ms

  • Configuration
    • Core Pool Size: 10
    • Max Pool Size: 20
    • Queue Capacity: 25
  • Processing
    1. Initialization: 10 threads are available and queue is empty(0/25)
    2. First 10 requests(1-10): 10 threads are processing the first 10 requests, queue is empty(0/25)
    3. Next 25 requests(11-35): with all 10 core threads busy, the next 25 requests are queued(25/25)
    4. Next 10 requests(36-45):
      • The queue is full, so the executor creates additional threads up to the max pool size(20)
      • Requests 36–45 are assigned to 10 new threads(total 20 threads: 10 core + 10 additional)
    5. Request 46-200:
      • The queue is full (25 tasks), and the max pool size is reached(20 threads)
      • Any additional tasks (requests 46–200) cannot be processed or queued
      • The default AbortPolicy kicks in, throwing a RejectedExecutionException for each of these 155 requests