Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 310
Rocky4Ever Avatar asked Oct 20 '25 03:10

Rocky4Ever


1 Answers

  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().

like image 175
Vsevolod Nechaev Avatar answered Oct 21 '25 17:10

Vsevolod Nechaev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!