gpt4 book ai didi

multithreading - AsyncHttpClient创建多少线程?

转载 作者:行者123 更新时间:2023-12-02 13:36:17 26 4
gpt4 key购买 nike

我在代码中使用异步http客户端来异步处理GET响应
我可以同时运行100个请求。

我只在容器中的httpClient实例上使用

@Bean(destroyMethod = "close")
open fun httpClient() = Dsl.asyncHttpClient()

代码看起来像
fun method(): CompletableFuture<String> {
return httpClient.prepareGet("someUrl").execute()
.toCompletableFuture()
.thenApply(::getResponseBody)
}

它在功能上正常工作。在我的测试中,我使用具有相同网址的模拟端点。但是我的期望是所有请求都在多个线程中处理,但是在事件探查器中,我看到为AsyncHttpClient创建了16个线程,即使没有要发送的请求,它们也不会被破坏。

profiler screenshoot

我的期望是
  • 异步客户端
  • 的线程数将减少
    某些配置的超时后,
  • 线程将被销毁
  • 是否有一些选项可以控制asyncHttpClient可以创建多少个线程?

  • 我在期望中错过了什么吗?

    更新1
    我看到了有关 https://github.com/AsyncHttpClient/async-http-client/wiki/Connection-pooling的说明
    我在线程池上找不到任何信息

    更新2
    我还创建了执行此操作的方法,但是使用了处理程序和其他执行程序池

    实用方法看起来像
    fun <Value, Result> CompletableFuture<Value>.handleResultAsync(executor: Executor, initResultHandler: ResultHandler<Value, Result>.() -> Unit): CompletableFuture<Result> {
    val rh = ResultHandler<Value, Result>()
    rh.initResultHandler()

    val handler = BiFunction { value: Value?, exception: Throwable? ->
    if (exception == null) rh.success?.invoke(value) else rh.fail?.invoke(exception)
    }

    return handleAsync(handler, executor)
    }

    更新后的方法看起来像
    fun method(): CompletableFuture<String> {
    return httpClient.prepareGet("someUrl").execute()
    .toCompletableFuture()
    .handleResultAsync(executor) {
    success = {response ->
    logger.info("ok")
    getResponseBody(response!!)
    }
    fail = { ex ->
    logger.error("Failed to execute request", ex)
    throw ex
    }
    }
    }

    然后,我可以看到GET方法的结果是在线程池提供的线程中执行的(以前,结果是在“AsyncHttpClient-3-x”中执行的),但是仍为AsyncHttpClient创建了其他线程,并且没有将其销毁。

    最佳答案

    AHC有两种类型的线程:

  • 用于I / O操作。
    在您的屏幕上,它是AsyncHttpClient-x-x
    线程。 AHC将创建其中的 2 * core_number
  • 超时。
    在您的屏幕上,它是AsyncHttpClient-timer-1-1线程。应该
    一个

  • 来源:GitHub上的问题: https://github.com/AsyncHttpClient/async-http-client/issues/1658

    关于multithreading - AsyncHttpClient创建多少线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56343520/

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