Sobes.tech
Junior — Middle

Does Dart support parallel task execution or does it only work in a single thread?

sobes.tech AI

Answer from AI

Dart supports parallel task execution, but not through traditional threads like in Java or C++. Instead, it uses a model called isolates — these are separate threads with their own memory space, which do not share state directly. Communication between isolates is done via messages.

Thus, Dart operates with a single main thread (UI thread in Flutter), but for heavy or parallel tasks, isolates can be created that run concurrently and do not block the main thread.

Example of creating an isolate:

import 'dart:isolate';

void heavyTask(SendPort sendPort) {
  // Performing a heavy task
  int result = 0;
  for (int i = 0; i < 100000000; 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();
  });
}
Does Dart support parallel task execution or does it… - sobes.tech