enum class CoroutineStart (source)
Defines start options for coroutines builders. It is used in start parameter of launch, async, and other coroutine builder functions.
The summary of coroutine start options is:
DEFAULT – immediately schedules coroutine for execution according to its context;
LAZY – starts coroutine lazily, only when it is needed;
ATOMIC – atomically (in a non-cancellable way) schedules coroutine for execution according to its context;
UNDISPATCHED – immediately executes coroutine until its first suspension point in the current thread.
job선언과 동시에 consume이 일어나는 경우가 많다.
그럴땐 LAZY를 사용한다.
val time = measureTimeMillis {
val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() }
val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() }
// some computation
one.start() // start the first one
two.start() // start the second one
println("The answer is ${one.await() + two.await()}")
}
println("Completed in $time ms")