Reputation: 1058
I am programming my Systems Programming course assignment (which uses Threads).
My active classes implement runnable, and I have another class with an executor that executes that runnable using a : ThreadPoolExecutor e = (ThreadPoolExecutor) Executors.newFixedThreadPool(number);
I have several threads running at the same time in that thread pool. My question is as follows: I have the option to know when one thread has finished its' job regarding what it needs to do, how can I shut this thread down, and -only- this thread, not the whole executor?
Upvotes: 0
Views: 1546
Reputation: 19185
If I have understood it correctly what you want is newCachedThreadPool
instead of newFixedThreadPool
From Java doc
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.
Upvotes: 0
Reputation: 200138
ThreadPoolExecutor
is in charge of managing its threads and there is no way for you to access them, except by some ugly hacks that will probably break the executor. What you should use is configuration options provided by the API, such as setKeepAliveTime
or the constructor that accepts keepAliveTime
. Set this to zero and you have the behavior you want, automatically.
Upvotes: 1