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 showUserOrders(username: String, password: String) { | |
val user = login(username, password) | |
val orders = fetchUserOrders(user.userId) | |
showUserOrders(orders) | |
} | |
fun login(username: String, password: String) { } | |
fun fetchUserOrders(userId: Long) { } |
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 showUserOrders(username: String, password: String) { | |
login(username, password) { | |
user -> fetchUserOrders(user.userId) { | |
orders -> showUserOrders(orders) | |
} | |
} | |
} | |
fun login(username: String, password: String, callback: (User) -> Unit) |
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 showUserOrders(username: String, password: String) { | |
login(username, password) | |
.flatMap { user -> fetchUserOrders(user.userId) } | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe { showOrders(it) } | |
} | |
fun login(username: String, password: String): Single<User> |
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 showUserOrders(username: String, password: String) = GlobalScope.launch(Dispatchers.Main) { | |
val user = withContext(Dispatchers.Default) { login(username, password) } | |
val orders = withContext(Dispatchers.Default) { fetchUserOrders(user.userId) } | |
showUserOrders(orders) | |
} | |
suspend fun login(username: String, password: String) { } | |
suspend fun fetchUserOrders(userId: Long) { } |
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(args: Array<String>) = runBlocking { | |
repeat(100000) { | |
launch { | |
delay(100) | |
println("coroutine $it") | |
} | |
} | |
} |
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
public fun withContext( | |
context: CoroutineContext, | |
block: suspend CorotuineScope.() -> T | |
): T |
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 (args: Array<String>) = runBlocking { | |
println("current thread: ${Thread.currentThread().name}") | |
//switch to default Dispatcher | |
val result = withContext(Dispatchers.Default) { | |
delay(500) | |
println("Thread: ${Thread.currentThread().name}") | |
100 | |
} |
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 CoroutineScope.async( | |
context: CoroutineContext = EmptyCorotuineContext, | |
start: CoroutineStart = CoroutineStart.DEFAULT, | |
block: suspend CoroutineScope.() -> Unit | |
): Deferred<T> |
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(args: Array<String>) = runBlocking { | |
launch { | |
delay(1_000) | |
println("from launch coroutine body") | |
} | |
println("from runblocking") | |
} | |
from runblocking | |
from launch coroutine body |
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(args: Array<String>) = runBlocking { | |
val time = measureTimeMillis { | |
val first = async { firstNumber() } | |
val second = async { secondNumber() } | |
val third = async { thirdNumber() } | |
val result = first.await() + second.await() + third.await() | |
} | |
println(time) //prints 7 seconds |
OlderNewer