简体   繁体   English

withTimeout() 是协程构建器吗? 它会阻塞调用线程吗?

[英]Is withTimeout() a coroutine builder? Does it block the calling thread?

  1. There are three main coroutine builders launch, async, runBlocking.有三个主要的协程构建器 launch、async、runBlocking。 My question is about withTimeout() is it a coroutine builder?.我的问题是关于withTimeout()它是协程构建器吗? if it is, what does it return?如果是,它会返回什么? what is its scope?它的范围是什么? what is its dispatcher?它的调度员是什么?

  2. I have following piece of code:我有以下代码:

     val myScope = CoroutineScope(Dispatchers.Default) runBlocking{ myScope.launch { //coroutine#1 Log.i(TAG, "First coroutine launched") } withTimeout(3000){ delay(2000) Log.i(TAG, "withTimeout block executed") } myScope.launch { //coroutine#2 Log.i(TAG, "Second coroutine launched") } }

The output is:输出是:

First coroutine launched
withTimeout block executed
Second coroutine launched

Here you can see withTimeout takes two seconds to complete the task but still coroutine#2 executed after this block.在这里您可以看到withTimeout需要两秒钟才能完成任务,但在此块之后仍然执行 coroutine#2。 It means that withTimeout blocks the calling thread.这意味着withTimeout阻塞了调用线程。 If it does not block the calling thread then output should be like this:如果它没有阻塞调用线程,那么输出应该是这样的:

First coroutine launched
Second coroutine launched
withTimeout block executed

Am I right?我对吗? please help me in understanding this scenario.请帮助我理解这种情况。 Thank you谢谢

withTimeout() is a suspend function so it should be invoked from a coroutine or another suspend function. withTimeout()是一个suspend函数,因此应该从协程或另一个suspend函数中调用它。

Coroutine builder is a bridge from the normal to the suspending world.协程构建器是从正常世界到悬浮世界的桥梁。

There are three main coroutine builders launch, async, runBlocking.有三个主要的协程构建器 launch、async、runBlocking。 My question is about withTimeout() is it a coroutine builder?.我的问题是关于 withTimeout() 它是协程构建器吗? if it is, what does it return?如果是,它会返回什么? what is its scope?它的范围是什么? what is its dispatcher?它的调度员是什么?

According to the docs: "Runs a given suspending block of code inside a coroutine ...", I've used logs to check, and it appears that it uses the current coroutine to execute the block of code, but it also waits for it to finish.根据文档:“在协程中运行给定的暂停代码块......”,我使用日志进行检查,似乎它使用当前协程来执行代码块,但它也在等待它完成。 withTimeout() is not a coroutine builder, because it is a suspend function by itself and it's not a bridge from the normal to the suspending world. withTimeout()不是协程构建器,因为它本身就是一个suspend函数,它不是从正常世界到挂起世界的桥梁。
It returns whatever the block , passed as the last parameter, returns.它返回作为最后一个参数传递的block返回的任何内容。 It creates another scope ( TimeoutCoroutine ) and uses it as a receiver.它创建另一个范围( TimeoutCoroutine )并将其用作接收器。 It uses the current context's CoroutineDispatcher .它使用当前上下文的CoroutineDispatcher

It means that withTimeout blocks the calling thread这意味着 withTimeout 阻塞了调用线程

withTimeout() is a suspend function, it doesn't block the thread, but suspends the coroutine it is launched in until the block is finished execution, or timeout fires. withTimeout()是一个suspend函数,它不会阻塞线程,而是挂起它启动的协程,直到block执行完毕或超时触发。 If block executes longer than provided timeout, then an exception will be thrown and coroutine#2 will not be executed.如果block执行的时间比提供的超时时间长,那么将抛出异常并且coroutine#2将不会被执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM