Rocky4Ever
Rocky4Ever

Reputation: 868

How Thread Life Cycle works inside Thread Pool?

Could anyone explain how thread life cycle works inside thread pool.Is it same as normal thread life cycle?

Upvotes: 4

Views: 1589

Answers (1)

Vsevolod Nechaev
Vsevolod Nechaev

Reputation: 41

  1. ThreadPool can wrap your Runnable/Callable with Future*(if you use submit() method)
  2. Runnable/Callable faces with task queue and saturation policy
    • behaviour depends on:
      • number of active threads in pool
      • corePoolSize
      • maximumPoolSize
      • saturation of queue
      • saturation policy(default is AbortPolicy)
  3. ThreadFactory creates Thread. Can be configured:
    • set UncaughtExceptionHandler
    • set name of Thread
    • set daemon flag
  4. beforeExecute() - empty hook method, you can override it
  5. Running your Thread
  6. afterExecute()** - empty hook method, you can override it
  7. Thread may be terminated or waits for processing new task. Configured by allowCoreThreadTimeOut()

* using submit() method changes code for afterExecute() - Handling exceptions from Java ExecutorService tasks
**afterExecute() will be not invoked if beforeExecute() throw any Exception

Note: part of behaviour of thread pool is similar to pattern template method - beforeExecute() -> run() -> afterExecute().

Upvotes: 2

Related Questions