Last active
April 21, 2021 09:58
-
-
Save davidmigloz/2bbc346e4bd93bab6f9a66a6a6b66e0f to your computer and use it in GitHub Desktop.
Dart vs Kotlin: threading
This file contains hidden or 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 'dart:isolate'; | |
void compute(int num) { | |
final isolateName = Isolate.current.debugName; | |
print('Isolate $isolateName computing $num...'); | |
} | |
main() async { | |
print('Spawning...'); | |
final isoFut1 = Isolate.spawn(compute, 1, debugName: "I1"); | |
final isoFut2 = Isolate.spawn(compute, 2, debugName: "I2"); | |
print('Waiting...'); | |
await isoFut1; | |
await isoFut2; | |
print('Done!'); | |
} |
This file contains hidden or 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 kotlinx.coroutines.* | |
suspend fun compute( | |
num: Int | |
) = withContext(Dispatchers.Default) { | |
val threadName = Thread.currentThread().name | |
println("Thread $threadName computing $num...") | |
} | |
fun main() = runBlocking { | |
println("Launching...") | |
val co1Job = launch { compute(1) } | |
val co2Job = launch { compute(2) } | |
println("Waiting...") | |
co1Job.join() | |
co2Job.join() | |
println("Done!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment