gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:21:19 25 4
gpt4 key购买 nike

我们的线程池中有大量线程无限期地等待连接,因为我们的 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) 并将 ma​​yInterruptIfRunning 标志 设置为 true 以在一段时间后终止此​​类长时间运行的线程。这些线程仍在等待连接并且没有被释放。

问题: 为什么这些线程没有被 future.cancel 清除?如果 future.cancel 不会释放这些线程,有哪些替代步骤可以杀死此类等待无用的线程?

添加更多关于实现的信息

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

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

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

}

最佳答案

您的线程正在等待 MultiThreadedHttpConnectionManager.doGetConnection 中的 synchronized (connectionPool) 监视器,它不负责中断。根据 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
*/

关于Java 线程转储 : java. lang.Thread.State:WAITING(在对象监视器上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47927240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com