okhttp 运行 在线程上或在 ktor 应用程序上协程

okhttp run on thread or coroutine on ktor applcation

我在我的服务器应用程序上使用 okhttp 和 ktor 并且 运行 像这样。

runBlocking {
    launch {
        okhttpClient.get(url)
    }
}

这是记录结果。

2021-05-17 11:18:21.620 DEBUG 11928 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : GET "/test/test2", parameters={}
2021-05-17 11:18:21.621 DEBUG 11928 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.kotlinstudy.TestController#test2()
2021-05-17 11:18:21.788 DEBUG 11928 --- [alhost:8081/...] okhttp3.internal.concurrent.TaskRunner   : Q10000 scheduled after   0 µs: OkHttp ConnectionPool
2021-05-17 11:18:21.789 DEBUG 11928 --- [Http TaskRunner] okhttp3.internal.concurrent.TaskRunner   : Q10000 starting              : OkHttp ConnectionPool
2021-05-17 11:18:21.789 DEBUG 11928 --- [ ConnectionPool] okhttp3.internal.concurrent.TaskRunner   : Q10000 run again after 300 s : OkHttp ConnectionPool
2021-05-17 11:18:21.789 DEBUG 11928 --- [Http TaskRunner] okhttp3.internal.concurrent.TaskRunner   : Q10000 finished run in 287 µs: OkHttp ConnectionPool
2021-05-17 11:18:21.828 DEBUG 11928 --- [@call-context#5] okhttp3.internal.concurrent.TaskRunner   : Q10000 scheduled after   0 µs: OkHttp ConnectionPool
2021-05-17 11:18:21.828 DEBUG 11928 --- [Http TaskRunner] okhttp3.internal.concurrent.TaskRunner   : Q10000 starting              : OkHttp ConnectionPool
2021-05-17 11:18:21.828 DEBUG 11928 --- [ ConnectionPool] okhttp3.internal.concurrent.TaskRunner   : Q10000 run again after 300 s : OkHttp ConnectionPool
2021-05-17 11:18:21.828 DEBUG 11928 --- [Http TaskRunner] okhttp3.internal.concurrent.TaskRunner   : Q10000 finished run in  92 µs: OkHttp ConnectionPool
2021-05-17 11:18:21.883 DEBUG 11928 --- [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/plain', given [*/*] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json, application/json, application/*+json]
2021-05-17 11:18:21.883 DEBUG 11928 --- [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Writing ["test1"]
2021-05-17 11:18:21.890 DEBUG 11928 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

已启动 tomcat nio 线程和 运行ning okhttp 线程。 你知道协程是轻线程。我希望 运行 阻塞代码在协程线程上工作。 但它似乎是另一个线程。

协程线程上的 httpClent 运行 好吗?还是普通线程? 使用协程线程应该怎么做?
使用 CIO 客户端?

谢谢大家。

这是CIO通话记录。看起来像工作协程线程

2021-05-17 13:05:16.271 DEBUG 20068 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : GET "/test/test3", parameters={}
2021-05-17 13:05:16.273 DEBUG 20068 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.kotlinstudy.TestController#test3()
2021-05-17 13:05:16.306  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : REQUEST: http://localhost:8081/server1
2021-05-17 13:05:16.307  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : METHOD: HttpMethod(value=GET)
2021-05-17 13:05:16.307  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : COMMON HEADERS
2021-05-17 13:05:16.308  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : -> Accept: application/json
2021-05-17 13:05:16.309  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : -> Accept-Charset: UTF-8
2021-05-17 13:05:16.309  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : -> User-Agent: Ktor client/1.3.1(npay-point)
2021-05-17 13:05:16.309  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : CONTENT HEADERS
2021-05-17 13:05:16.309  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : -> Content-Length: 0
2021-05-17 13:05:16.423  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : RESPONSE: 200 OK
2021-05-17 13:05:16.423  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : METHOD: HttpMethod(value=GET)
2021-05-17 13:05:16.423  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : FROM: http://localhost:8081/server1
2021-05-17 13:05:16.423  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : COMMON HEADERS
2021-05-17 13:05:16.423  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : -> Content-Length: 20
2021-05-17 13:05:16.423  INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient                : -> Content-Type: text/plain; charset=UTF-8
Hello world! server1
2021-05-17 13:05:16.467 DEBUG 20068 --- [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/plain', given [*/*] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json, application/json, application/*+json]
2021-05-17 13:05:16.468 DEBUG 20068 --- [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Writing ["test1"]
2021-05-17 13:05:16.475 DEBUG 20068 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed 200 OK


尽管大多数人将协程视为轻量级线程,但实际上并非如此。协程在线程中启动,然后挂起直到它准备好恢复。参见:Coroutines basics:

...a coroutine is not bound to any particular thread. It may suspend its execution in one thread and resume in another one.

协程并非旨在坚持一个线程并仅使用该线程。您需要阐明您需要它的用例,因为它可能更像是一个代码设计决策。