演示kotlin的structured concurrency简化并发异步操作

本文介绍了如何在SpringBoot项目中使用Java.net.http.HttpClient进行同步和异步HTTP调用,展示了StructuredConcurrency如何简化并发处理,并通过示例分析了其工作原理。同步调用会阻塞线程,而异步调用通过CompletableFuture实现非阻塞,利用了操作系统事件通知机制提升并发性能。
摘要由CSDN通过智能技术生成


用springboot的 kotlin demo,帮助理解structured concurrency简化异步并发调用的机制

准备http客户端

使用同时支持同步和异步调用的java.net.http.HttpClient

@Configuration
class RestTemplateConfig {
    @Bean
    fun httpClient(): HttpClient {
        val client: HttpClient = HttpClient.newBuilder()
            .connectTimeout(Duration.ofSeconds(3))
            .followRedirects(HttpClient.Redirect.NEVER) // CompletableFuture的默认回调线程池也是这个
            .executor(ForkJoinPool.commonPool())
            .build()
        return client
    }

    @Bean
    @Primary
    fun restTemplate(httpClient: HttpClient,
        builder: RestTemplateBuilder
    ): RestTemplate {
        // 使用springboot自动初始化的builder,已加载各种RestTemplateRequestCustomizer
        return builder.requestFactory(Supplier {
            JdkClientHttpRequestFactory(
                httpClient
            )
        }).build()
    }
}       

同步调用

@AutoConfigureMockMvc
@SpringBootTest
class ApplicationTests {
    private val log: Logger = LoggerFactory.getLogger(this::class.java)
    @Autowired
    private lateinit var httpClient: HttpClient
    suspend fun blockingGet(tag: String) = withContext(Dispatchers.IO) {
        // 在Dispatchers.IO线程池上执行
        log.info("$tag 同步http调用")
        var threadName1 = Thread.currentThread().name
        val req1 = HttpRequest.newBuilder()
            .GET().timeout(Duration.ofSeconds(10))
            .uri(URI.create("https://xxx"))
            .build()

        var resp: HttpResponse<String> = httpClient.send(req1, HttpResponse.BodyHandlers.ofString())
        log.info("$tag 等待异步http调用完成: ${resp.body()}")
        var threadName2 = Thread.currentThread().name
        // 证明IO调用期间,线程被阻塞而没有释放
        assertEquals(threadName1, threadName2)
        log.info("$tag $threadName1 -> $threadName2")
    }
}

异步调用

    suspend fun asyncGet(tag: String) = withContext(Dispatchers.IO) {
        // 在Dispatchers.IO线程池上执行
        log.info("$tag 异步http调用")
        var threadName1 = Thread.currentThread().name
        val req1 = HttpRequest.newBuilder()
            .GET().timeout(Duration.ofSeconds(10))
            .uri(URI.create("https://xxx"))
            .build()

        val future1: CompletableFuture<HttpResponse<String>> =
            httpClient.sendAsync(req1) { info: ResponseInfo ->
                // 接收完response header后,触发这里的逻辑执行;然后再解析body,再完成future1
                log.info("$tag Protocol: ${info.version()}, ${Thread.currentThread().name}")
                BodySubscribers.ofString(StandardCharsets.UTF_8)
            }

        var resp: HttpResponse<String> = future1.await()
        // 上面调用了suspend方法,于是编译器把下面的代码包装到状态机的某个状态的回调里
        var threadName2 = Thread.currentThread().name
        // 能够观察到线程切换,证明future1.await()这个suspend之后的代码是异步回调的
        log.info("$tag $threadName1 -> $threadName2")
        log.info("$tag 等待异步http调用完成: ${resp.body()}")
    }

HttpClient的send实质是对sendAsync的封装,是在调用线程上强制等待sendAsync返回的CompletableFuture完成。

structured concurrency用例与机制浅析

    @Test
    fun test1() {
        log.info("main begin: " + Thread.currentThread().toString())
        var dispatcher: ExecutorCoroutineDispatcher = ForkJoinPool.commonPool().asCoroutineDispatcher()
        var result1 = runBlocking(dispatcher) {
            // runBlocking{}内的代码全部在dispatcher提供的线程上执行,launch{}里的coroutine除外
            var threadName1 = Thread.currentThread().name
            repeat(3) {
                launch { blockingGet("blocking request" + it) }
            }
            repeat(3) {
                launch { asyncGet("async request" + it) }
            }
            // 调用一个suspend方法
            delay(2L)
            var threadName2 = Thread.currentThread().name
            delay(2L)
            var threadName3 = Thread.currentThread().name
            // delay这个suspend真实引发了线程释放和回调,所以每个delay前后的代码不在同一个线程执行
            log.info("block end: $threadName1 -> $threadName2 -> $threadName3")
            "block result1"
        }
        // runBlocking阻塞调用它main线程到这里
        log.info(result1)
        log.info("main end: " + Thread.currentThread().toString())
    }

demo里3个blockingGet+3个asyncGet都是并发执行的。
但是blockingGet内部使用的是阻塞式API,实际长期占用线程来等待IO结果,并没有释放线程资源来达到提升并发能力的目的。
asyncGet内部实际发生了线程的释放和异步回调;kotlin编译器在coroutine内检测到对suspend方法的调用(就是future1.await()调用),就会生成匿名类/状态机,来分割调用前后的执行逻辑;因为这个future1的完成最终是依赖IOCP/ePOll等操作系统IO接口的事件通知,所以在等待过程中不占用/阻塞任何操作系统线程;通过打印的日志能观察到future1.await()调用前后的执行线程是不同的。

runBlocking{}则阻塞调用它的线程,等待runBlocking{}内部所有launch的coroutine完成。

补充说明

阻塞式http调用,实际也是依赖操作系统的IO事件,只是没释放等待前的线程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值