Junior — Middle
Kas sa saad selgitada, mis on isolaatoreid programmeerimise kontekstis?
sobes.tech AI
Vastus AI-lt
In Flutter, isolates are separate execution threads that do not share memory with the main thread. They allow performing heavy or long-running operations in parallel without blocking the UI. Each isolate has its own memory and communicates with others via message passing.
Example of creating an isolate:
import 'dart:isolate';
void heavyTask(SendPort sendPort) {
// Perform a heavy operation
int result = 0;
for (int i = 0; i < 1000000; i++) {
result += i;
}
sendPort.send(result);
}
void main() async {
ReceivePort receivePort = ReceivePort();
await Isolate.spawn(heavyTask, receivePort.sendPort);
receivePort.listen((message) {
print('Result from isolate: $message');
receivePort.close();
});
}
This way, isolates help efficiently utilize multithreading in Dart/Flutter and prevent issues with concurrent memory access.