Skip to content

Instantly share code, notes, and snippets.

@kipsong133
Last active February 27, 2024 04:42
Show Gist options
  • Save kipsong133/fdc4a3d61600cdadc0d0130d0b1d5638 to your computer and use it in GitHub Desktop.
Save kipsong133/fdc4a3d61600cdadc0d0130d0b1d5638 to your computer and use it in GitHub Desktop.
flutter_isolate_example_1.dart
import 'dart:isolate';

String mainIsolate = '';
String newIsolate = '';
String get isMainIsolate =>
    (mainIsolate == Isolate.current.hashCode.toString()) ? 'πŸ”΄' : 'πŸ”΅';

void main() async {
  mainIsolate = Isolate.current.hashCode.toString();

  print('메인 Isolate μ‹œμž‘');
  ReceivePort receivePort = ReceivePort();
  await Isolate.spawn(heavyTask, receivePort.sendPort);
  receivePort.listen(
    (message) {
      // λ¦¬μŠ€λ„ˆ λ™μž‘μ€ 메인 Isolateμ—μ„œ λ™μž‘ν•œλ‹€
      print('$isMainIsolate λ¦¬μŠ€λ„ˆ λ™μž‘');
      print('$isMainIsolate 받은 λ©”μ‹œμ§€: $message');
      print('$isMainIsolate μ’…λ£Œ: ${Isolate.current.hashCode}');
    },
  );
}

void heavyTask(SendPort port) {
  // heavyTask ν•¨μˆ˜λŠ” μƒˆλ‘œμš΄ Isolateμ—μ„œ λ™μž‘ν•œλ‹€
  print('$isMainIsolate μƒˆ Isolate μ‹œμž‘: ${Isolate.current.hashCode}');
  newIsolate = Isolate.current.hashCode.toString();
  port.send('무거운 μž‘μ—…μ΄ μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€ :)');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment