Could anyone explain how thread life cycle works inside thread pool.Is it same as normal thread life cycle?
Asked
Active
Viewed 1,063 times
4
-
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 Answers
2
- ThreadPool can wrap your Runnable/Callable with Future*(if you use submit() method)
- 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)
- behaviour depends on:
- ThreadFactory creates Thread. Can be configured:
- set UncaughtExceptionHandler
- set name of Thread
- set daemon flag
- beforeExecute() - empty hook method, you can override it
- Running your Thread
- afterExecute()** - empty hook method, you can override it
- 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().

Vsevolod Nechaev
- 41
- 5