Async Programming

 0    9 schede    H3TM4N
Scarica mp3 Stampa Gioca Testa il tuo livello
 
Domanda język polski Risposta język polski
Kotlin Coroutines
inizia ad imparare
asynchronous or non-blocking programming
Coroutines
inizia ad imparare
are computer program components that generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed.
It is conceptually similar to a thread, in the sense that it takes a block of code to run that works concurrently with the rest of the code. However, a coroutine is not bound to any particular thread. It may suspend its execution in one thread and resume.
Coroutines as light-weight threads
inizia ad imparare
Coroutines can be thought of as light-weight threads, but there is a number of important differences that make their real-life usage very different from threads.
launch
inizia ad imparare
is a coroutine builder. It launches a new coroutine concurrently with the rest of the code, which continues to work independently.
delay
inizia ad imparare
is a special suspending function. It suspends the coroutine for a specific time. Suspending a coroutine does not block the underlying thread, but allows other coroutines to run and use the underlying thread for their code.
runBlocking
inizia ad imparare
is also a coroutine builder that bridges the non-coroutine world of a regular fun main() and the code with coroutines inside of runBlocking {...} curly braces.
suspending function
inizia ad imparare
can be used inside coroutines just like regular functions, but their additional feature is that they can, in turn, use other suspending functions
Scope builder
inizia ad imparare
It creates a coroutine scope and does not complete until all launched children complete.
runBlocking and coroutineScope builders may look similar because they both wait for their body and all its children to complete. The main difference is that the runBlocking method blocks the current thread for waiting, while coroutineScope just suspends.
Job
inizia ad imparare
A launch coroutine builder returns a Job object that is a handle to the launched coroutine and can be used to explicitly wait for its completion. For example, you can wait for completion of the child coroutine and then print "Done" string:
val job = launch {// launch a new coroutine and keep a reference to its Job delay(1000L) println("World!")}

Devi essere accedere per pubblicare un commento.