4

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

Rocky4Ever
  • 828
  • 3
  • 12
  • 35
  • Do you actually meant to tag j, as in the j programming language by jsoftware? They're entirely different from the Java programming language – Martheen Apr 20 '20 at 00:23
  • Sorry i accidentally tagged j Edited my tags.Thanks! – Rocky4Ever Apr 20 '20 at 00:52
  • A thread is a thread, whether it's in a pool or not. The thread pool creates the thread when necessary, the thread waits for a task by, for example, blocking on a queue until either a task arrives, in which case the thread executes the task and goes back to waiting for another task, or the configured "max idle time" / "keep alive time" expires and the pool lets the thread die. Each thread in the pool is doing this in parallel (typically using the same queue). – Slaw Apr 20 '20 at 02:26

1 Answers1

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