This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() = runBlocking { | |
println("Start") | |
val job = GlobalScope.launch { | |
delay(1000L) | |
println("Hello") | |
} | |
job.join() | |
println("Done") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() = runBlocking { | |
println("Start") | |
GlobalScope.launch { | |
delay(1000L) | |
println("Hello") | |
} | |
delay(2000L) | |
println("Done") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
println("Start") | |
// Start a coroutine | |
GlobalScope.launch { | |
delay(1000L) | |
println("Hello") | |
} | |
runBlocking { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Continuation<in T> { | |
val context: CoroutineContext | |
fun resume(value: T) | |
fun resumeWithException(exception: Throwable) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun backgroundTask(param: Int, callback: Continuation<Int>): Int { | |
// long running operation | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun backgroundTask(param: Int): Int { | |
// long running operation | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
println("Start") | |
// Start a coroutine | |
GlobalScope.launch { | |
delay(1000L) | |
println("Hello") | |
} | |
Thread.sleep(2000L) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
println("Start") | |
Thread { | |
Thread.sleep(3000L) | |
println("Hello") | |
}.start() | |
println("Done") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dispatchers.Main | |
suspend fun fetchDocs() { | |
// Dispatchers.Main | |
val result = get("developer.android.com") | |
// Dispatchers.Main | |
show(result) | |
} | |
// Dispatchers.Main | |
suspend fun get(url: String) = | |
// Dispatchers.Main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dispatchers.Main | |
suspend fun fetchDocs() { | |
// Dispatchers.Main | |
val result = get("developer.android.com") | |
// Dispatchers.Main | |
show(result) | |
} | |
// look at this in the next section | |
suspend fun get(url: String) = withContext(Dispatchers.IO){/*...*/} |