Java 线程转储:java.lang.Thread.State:WAIT(在对象监视器上)



我们的线程池中有大量线程无限期地等待连接,因为我们的 httpclient 没有任何超时。

线程转储:

"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)

但是我们调用future.cancel(true)并将mayInterruptIfRunning 标志设置为 true,以便在一段时间后终止此类长时间运行的线程。这些线程仍在等待连接,并且没有被释放。

问题:为什么这些线程没有被future.cancel清理?如果 future.cancel 不能释放这些线程,那么杀死这些等待徒劳的线程的替代步骤是什么?

添加有关实现的更多信息

我无法分享确切的代码,但提供了一些模拟示例

我们的 ThreadPoolexecutor 具有 Unbounded LinkedBlockingQueue,我们未来的任务都是可调用的,我们正在使用executor.submit(callable)来执行我们的任务。

public class MockThreadPoolExecutor extends ThreadPoolExecutor {
public MockThreadPoolExecutor(int numThread) {
super(numThread,numThread, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
prestartAllCoreThreads();
}
}

您的线程正在等待synchronized (connectionPool)监视器,MultiThreadedHttpConnectionManager.doGetConnection不负责中断。根据getConnectionWithTimeout越来越多的maxHostConnectionsmaxTotalConnections可以提供帮助的文档。也可以在http.connection-manager.timeout中指定超时值,默认情况下为 0,以便线程无限期地等待连接。

/**
* Gets a connection or waits if one is not available.  A connection is
* available if one exists that is not being used or if fewer than
* maxHostConnections have been created in the connectionPool, and fewer
* than maxTotalConnections have been created in all connectionPools.
*
* @param hostConfiguration The host configuration specifying the connection
*        details.
* @param timeout the number of milliseconds to wait for a connection, 0 to
* wait indefinitely
*
* @return HttpConnection an available connection
*
* @throws HttpException if a connection does not become available in
* 'timeout' milliseconds
* 
* @since 3.0
*/

最新更新