1

Java executor has a pool of threads. 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?

On the other hand, if I want to terminate thread, how to terminate thread?

user84592
  • 4,750
  • 11
  • 55
  • 91
  • 2
    Isn't the idea of an executor / thread pool to not worry about these things and let the lib handle the icky bits? :) – akaIDIOT Feb 06 '13 at 08:19
  • @akaIDIOT Sometimes it's better to control the number of threads your application uses. Excessive number of threads, especially ones that are not used, is just pointless and waste of resources. – Adam Dyga Feb 06 '13 at 08:24
  • @AdamDyga Well, yes, of course. With the provided context, however, we're guessing as to *why* OP wants to do this. In case he/she was needlessly worried about things: dealing with keeping the thread count of the pool at 8 is the lib's responsibility (as you pointed out below :)) – akaIDIOT Feb 06 '13 at 08:30
  • @akaIDIOT Right, I meant controlling the number of threads by keeping or shutting down the entire `ExcecutorService`. But that's something out of the scope of this question – Adam Dyga Feb 06 '13 at 08:33
  • Ok, I give more explanation. I have a shared userid pool. I want threads to access it within its range. So I assign every thread an id, then every threads manipulate its own range in the useridpool according to its thread id since userid shall be unique to each user. I need to know when thread is dead, then the new thread could take over the dead thread id. – user84592 Feb 06 '13 at 08:56

3 Answers3

4

Yes, they will live forever as long as the ExecutorService exists. You can shut them down by calling ExecutorService.shutdown(). As for the other part of your question, the javadoc explains it all:

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.

Adam Dyga
  • 8,666
  • 4
  • 27
  • 35
  • Executor normally manage threads pool. How could client knows that one thread is dead and the new thread is created? Since I want new thread takes/copies some attributes of old thread. – user84592 Feb 06 '13 at 08:59
  • @user84592 Passing data between threads (tasks) is out of the scope of Executors. You have to do it on your own, eg. by making use of `Future` objects (and `get()` method) – Adam Dyga Feb 06 '13 at 10:36
4

Shortly speaking thread has the following life cycle phases:

  1. Thread myThread = new Thread(); - thread is created. It is not running yet.
  2. myThread.start() - now thread is started and its run() method is executed.
  3. When run() method is terminated the thread is exited. Once thread is terminated it cannot be started again.

Simple thread pools typically start N threads that are just waiting for tasks by executing method wait() into their run(). When task is submitted the dispatcher of thread pool gives it to one of the threads by enqueing it and calling the thread's notify() so that wait() exits and thread starts to perform the task. Once it is done thread returns to waiting state.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

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

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211