There are three main coroutine builders launch, async, runBlocking. My question is about withTimeout() is it a coroutine builder?. if it is, what does it return? what is its scope? what is its dispatcher?
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. It means that withTimeout blocks the calling thread. 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 asuspend
function so it should be invoked from a coroutine or anothersuspend
function.Coroutine builder is a bridge from the normal to the suspending world.
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 asuspend
function by itself and it's not a bridge from the normal to the suspending world.It returns whatever the
block
, passed as the last parameter, returns. It creates another scope (TimeoutCoroutine
) and uses it as a receiver. It uses the current context'sCoroutineDispatcher
.withTimeout()
is asuspend
function, it doesn't block the thread, but suspends the coroutine it is launched in until theblock
is finished execution, or timeout fires. Ifblock
executes longer than provided timeout, then an exception will be thrown andcoroutine#2
will not be executed.