Query 1:
If I have fixed length of pool (8 for example). Does all these 8 threads would live forever if application does not terminate? If some part of codes throw exception, does the thread in the pool would be dead?
I assume that you are talking about fixed thread pool. Refer to newFixedThreadPool API in Executors
:
public static ExecutorService newFixedThreadPool(int nThreads)
Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available.
If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.
The threads in the pool will exist until it is explicitly shutdown.
Query 2:
if I want to terminate thread, how to terminate thread?
Have a look at related SE question:
ExecutorService's shutdown() doesn't wait until all threads will be finished