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
launch(Dispatcher.Main){ | |
println("Download Started") | |
// launch(Dispatcher.Background) | |
withContext(Dispatcher.Background) { | |
println("Download in progress") | |
downloader.download() | |
} | |
println("Download Complete") | |
} |
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 <R> withContext(dispatcher: Dispatcher, block: suspend () -> R): R { | |
return suspendCoroutineUninterceptedOrReturn<R> { cont -> | |
val callback = object : Continuation<R> { | |
override val context = dispatcher | |
override fun resumeWith(result: Result<R>) { | |
when (cont.context as Dispatcher) { | |
Dispatcher.Main -> Handler(Looper.getMainLooper()).post { | |
cont.resumeWith(result) | |
} |
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 withContext(dispatcher: Dispatcher, block: suspend () -> Unit) { | |
// Suspend current coroutine | |
suspendCoroutineUninterceptedOrReturn<Unit> { cont -> | |
// Define completion callback for new coroutine | |
val callback = object : Continuation<Unit> { | |
override val context = EmptyCoroutineContext | |
override fun resumeWith(result: Result<Unit>) { | |
// Resume the previous coroutine | |
when (cont.context as Dispatcher) { |
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
launch(Dispatcher.Main){ | |
println("Download Started") | |
launch(Dispatcher.Background){ | |
println("Download in progress") | |
downloader.download(url) | |
} | |
println("Download Complete") | |
} | |
// Output |
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
import java.lang.Exception | |
import kotlin.concurrent.thread | |
import java.util.concurrent.Executors | |
import kotlin.coroutines.Continuation | |
import kotlin.coroutines.CoroutineContext | |
import kotlin.coroutines.intrinsics.createCoroutineUnintercepted | |
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn | |
import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED |
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 launch(dispatcher: Dispatcher, block: suspend () -> Unit) { | |
val callback = object : Continuation<Unit> { | |
// Passing dispatcher as CoroutineContext | |
override val context: CoroutineContext = dispatcher | |
override fun resumeWith(result: Result<Unit>) {} | |
} | |
val coroutine = block.createCoroutineUnintercepted(callback) | |
when (dispatcher) { | |
Dispatcher.Main -> Handler(Looper.getMainLooper()).post { | |
coroutine.resumeWith(Result.success(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
sealed class Dispatcher: CoroutineContext.Element { | |
object Main : Dispatcher() | |
object Background : Dispatcher() | |
override val key = DispatcherKey | |
} | |
object DispatcherKey: CoroutineContext.Key<Dispatcher>{} |
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() { | |
launch(Dispatcher.Background) { | |
fun1(Dispatcher.Background) | |
} | |
} | |
suspend fun fun1(dispatcher: Dispatcher){ | |
... | |
fun2(dispatcher) | |
... |
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
button.setOnClickListener{ | |
launch(Dispatcher.Main) { | |
textView.text = "Downloading file" // Runs on main thread | |
suspendCoroutineUninterceptedOrReturn<Unit> { cont -> | |
thread { | |
downloader.download("http://example.com/file.txt") // Runs on Background thread | |
cont.resumeWith(Result.success(Unit)) | |
} | |
COROUTINE_SUSPENDED | |
} |
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
sealed class Dispatcher { | |
object Main : Dispatcher() | |
object Background : Dispatcher() | |
} | |
fun launch(dispatcher: Dispatcher, block: suspend () -> Unit) { | |
//Define a callback | |
val callback = object : Continuation<Unit> { | |
override val context = EmptyCoroutineContext |