There are a large number of threads from our thread pool waiting for a connection indefinitely because our httpclient doesn't have any timeout.
Thread dump:
"pool-18-thread-400" #471 prio=5 os_prio=0 tid=0x00007fdf37a61000 nid=0x6ed7 in Object.wait() [0x00007fde8df9e000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007263acb18> (a org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$ConnectionPool)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.doGetConnection(Unknown Source)
- locked <0x00000007263acb18> (a org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$ConnectionPool)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.getConnectionWithTimeout(Unknown Source)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(Unknown Source)
at org.apache.commons.httpclient.HttpClient.executeMethod(Unknown Source)
But we are calling future.cancel(true) with mayInterruptIfRunning flag set to true to kill such long-running threads after some time. These threads are still waiting for connections and are not getting freed up.
Question: Why are these threads not cleaned up by future.cancel? If future.cancel would not free up these threads, What are the alternative steps to kill such kinds of threads waiting for futile?
Adding more information about implementations
I can't share the exact code but providing some mock example
Our ThreadPoolexecutor is having Unbounded LinkedBlockingQueue and our future tasks are all callables and we are using executor.submit(callable)
to execute our tasks.
public class MockThreadPoolExecutor extends ThreadPoolExecutor {
public MockThreadPoolExecutor(int numThread) {
super(numThread,numThread, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
prestartAllCoreThreads();
}
}