It is conceptually similar to a thread, in the sense that it takes a block of code to run and has a similar life-cycle — it is created and started, but it is not bound to any particular thread. It may suspend its execution in one thread and resume in another one. Moreover, like a future or promise, it may complete with some result (which is either a value or an exception).
Can start and cancel them § Is notified upon cancellations and failures § Defines when/where/how long coroutines exist Coroutine Builder Coroutine Context Coroutine Scope { ... }
(CoroutineContext) § Is provided through CoroutineScope.coroutineContext § Implemented as an index based set of elements (mixture of a set and a map) Coroutine Builder Coroutine Context Coroutine Scope { ... }
number of cores § Dispatchers.Main Execution only on the Main Thread § Dispatchers.IO For long running/blocking I/O operations § Dispatchers.Unconfined no restrictions (shouldn‘t be used)
suspending lambda as an argument, creates a coroutine, and, optionally, gives access to its result in some form. For example, launch{}, future{}, and sequence{} [..] are coroutine builders. The standard library provides primitive coroutine builders that are used to define all other coroutine builders. Coroutine Builder Coroutine Context Coroutine Scope { ... }
runBlocking, ... § extension functions to CoroutineScope § Can receive parameters, for example a launch mode Matt Walsh, Unsplash (https://unsplash.com/photos/tVkdGtEe2C4)
to DEFAULT; can be cancelled only after execution has started § LAZY launch only if needed (when result is accessed) § UNDISPATCHED Immediate execution on the current thread
have to run in a coroutine. It looks exactly like an ordinary lambda expression but its functional type is marked with suspend modifier. Just like a regular lambda expression is a short syntactic form for an anonymous local function, a suspending lambda is a short syntactic form for an anonymous suspending function. It may suspend execution of the code without blocking the current thread of execution by invoking suspending functions. For example, blocks of code in curly braces following launch, future, and sequence functions [...] are suspending lambdas. https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md#terminology
suspend modifier. It may suspend execution of the code without blocking the current thread of execution by invoking other suspending functions. A suspending function cannot be invoked from a regular code, but only from other suspending functions and from suspending lambdas [...]. For example, await() and yield() [...] are suspending functions that may be defined in a library. The standard library provides primitive suspending functions that are used to define all other suspending functions. https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md#terminology
where the execution of the coroutine may be suspended. Syntactically, a suspension point is an invocation of suspending function, but the actual suspension happens when the suspending function invokes the standard library primitive to suspend the execution. A continuation is a state of the suspended coroutine at suspension point. It conceptually represents the rest of its execution after the suspension point.
§ are paused at some time or have to wait for something to finish § do not block the current thread § are called from other coroutines or suspending functions § like normal functions return something Matt Walsh, Unsplash (https://unsplash.com/photos/tVkdGtEe2C4 Matt Walsh, Unsplash (https://unsplash.com/photos/tVkdGtEe2C4)
block and children § runBlocking blocks the current thread § coroutineScope suspends its execution § Difference: coroutine builder vs. suspending function
yield() or ensureActive() ... val job = launch { println("Enter") var count = 0 while (isActive) { println("${++count}") yield() } println("Exit") } ... • All suspending functions in kotlinx.coroutines are cancellable • Your suspend functions should be cancellable, too
somethig should be done when a result is available? § Deferred is a non blocking, cancellable Future § Created with async (Builder) or CompletableDeferred() § Same states like Job § Get result with await() (in case of an error an exception is thrown)
val scope = CoroutineScope(SupervisorJob()) § Uncaught exceptions are propagated upward § SupervisorJob works only when being the immediate parent of a coroutine