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

问题描述 投票:2回答:1

我们的线程池中有大量线程无限期地等待连接,因为我们的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,我们未来的任务都是callables,我们正在使用executor.submit(callable)来执行我们的任务。

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

}
java multithreading
1个回答
1
投票

你的线程正在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
 */
© www.soinside.com 2019 - 2024. All rights reserved.