Link: https://api.dart.dev/stable/2.7.1/dart-isolate/dart-isolate-library.html
Concurrent programming using isolates: independent workers that are similar to threads but don't share memory, communicating only via messages.
- Dart is Single Threaded
- Calculate so many on a threaded => Hang app
for (var i = 0; i < 1000000000000; i++) { total += i; }
- Speed up application
3. Kill isolate
// After 1s will kill isolate
Future.delayed(Duration(seconds: 1), () {
newIsolate.kill(priority: Isolate.immediate);
newIsolate = null;
print('Isolate is killed after 1s!');
});
Future<void> demoCompute() async {
// Calculate a new value
var result = await compute(calutate, 5);
print(result);
}
static int calutate(int number) {
return number * 10;
}