Executors threads not terminating
I am using Executors.newFixedThreadPool(100) method. Single command execution needs approx 20 threads. After executing the command 5-6 times, application stops responding. My thread is implementing Callable.
I doubt, that thread doesn't terminate 开发者_StackOverflowafter completion. I have also called shutdown() to terminate the thread.
Can anybody please tell, when I use get() method to retrieve the thread's result, does it gets terminated(means, its removed from the queue) or it is still there in the queue, which is used by pool to store the threads.
The threads don't terminate. What happens is this:
- All worker threads wait for the input queue
- One thread pops the head element from the queue
- It runs the
Callable
- It pushes the result into the result queue
- It waits for a new element in the input queue
So either the result queue overflows or your Callable
doesn't return.
get()
is a blocking call - that means the calling thread halts until the running thread has completed its task and the result is available.
The Executor takes care of taking tasks from the queue and cleaning up, so the answer is "no" - it isn't "still on the queue"
Caution using 100 threads - that's a ridiculously high number. Try between 2 and 8 for a typical machine (all depends on how much time is spent waiting for other things eg I/O - the more CPU bound your tasks are, the less threads you should use.
Even after calling get(), the thread would still be in the queue. Infact the API ensures that even if a thread dies, it recreates one to maintain "fixedness"(fixed no of threads in your pool)
Note that if the threads in your pool are actually doing some task(apart from waiting), calling shutdown will not terminate the thread. So, for e.g. if your threads are in a infinite loop doing something, then calling shutdown is useless.
Calling shutdown() only stops the thread pool from accepting new tasks and allows all thread to finish once all tasks have been performed.
How many cores do you have? If you have 100 busy threads, and say 4 cores every thread is only going to get a small amount of CPU time.
精彩评论