Skip to content

Instantly share code, notes, and snippets.

@davidmigloz
Last active April 21, 2021 09:58
Show Gist options
  • Save davidmigloz/2bbc346e4bd93bab6f9a66a6a6b66e0f to your computer and use it in GitHub Desktop.
Save davidmigloz/2bbc346e4bd93bab6f9a66a6a6b66e0f to your computer and use it in GitHub Desktop.
Dart vs Kotlin: threading
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!');
}
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