公告

👇欢迎大家关注公众号&私信交流👇

Skip to content

Flutter 多线程和并发编程

Isolate 基础

1. 什么是 Isolate

Isolate 是 Flutter 中的独立执行单元,每个 Isolate 都有自己的内存堆和事件循环,它们之间不共享内存。

2. 创建 Isolate

dart
void main() async {
  // 创建新的 Isolate
  final isolate = await Isolate.spawn(
    heavyComputation,
    'Hello from main isolate',
  );

  // 销毁 Isolate
  isolate.kill();
}

void heavyComputation(String message) {
  print('Received message: $message');
  // 执行耗时计算
}

3. Isolate 通信

dart
class IsolateMessage {
  final SendPort sendPort;
  final String data;

  IsolateMessage(this.sendPort, this.data);
}

void main() async {
  // 创建接收端口
  final receivePort = ReceivePort();

  // 创建 Isolate 并传递发送端口
  await Isolate.spawn(
    isolateFunction,
    IsolateMessage(receivePort.sendPort, 'Hello'),
  );

  // 监听消息
  receivePort.listen((message) {
    print('Received: $message');
  });
}

void isolateFunction(IsolateMessage message) {
  print('Processing: ${message.data}');
  message.sendPort.send('Task completed');
}

异步编程

1. Future 和 async/await

dart
Future<String> fetchData() async {
  // 模拟网络请求
  await Future.delayed(Duration(seconds: 2));
  return 'Data fetched';
}

void processData() async {
  try {
    final result = await fetchData();
    print(result);
  } catch (e) {
    print('Error: $e');
  }
}

2. Stream 处理

dart
Stream<int> countStream(int max) async* {
  for (int i = 1; i <= max; i++) {
    await Future.delayed(Duration(seconds: 1));
    yield i;
  }
}

void handleStream() async {
  final stream = countStream(5);
  await for (final value in stream) {
    print('Received: $value');
  }
}

并发控制

1. Compute 函数

dart
Future<List<int>> processDataInBackground(List<int> data) {
  return compute(heavyProcessing, data);
}

List<int> heavyProcessing(List<int> input) {
  // 执行耗时计算
  return input.map((e) => e * 2).toList();
}

// 使用示例
void example() async {
  final result = await processDataInBackground([1, 2, 3, 4, 5]);
  print('Processed data: $result');
}

2. 并发队列

dart
class TaskQueue {
  final Queue<Function> _tasks = Queue();
  bool _isProcessing = false;

  void addTask(Function task) {
    _tasks.add(task);
    _processNextTask();
  }

  Future<void> _processNextTask() async {
    if (_isProcessing || _tasks.isEmpty) return;

    _isProcessing = true;
    try {
      await _tasks.removeFirst()();
    } finally {
      _isProcessing = false;
      _processNextTask();
    }
  }
}

3. 并发限制

dart
class ConcurrencyLimiter {
  final int maxConcurrent;
  int _running = 0;
  final Queue<Function> _queue = Queue();

  ConcurrencyLimiter(this.maxConcurrent);

  Future<void> run(Future<void> Function() task) async {
    if (_running >= maxConcurrent) {
      _queue.add(task);
      return;
    }

    _running++;
    try {
      await task();
    } finally {
      _running--;
      if (_queue.isNotEmpty) {
        run(_queue.removeFirst());
      }
    }
  }
}

最佳实践

1. 性能优化

dart
class PerformanceOptimizer {
  static Future<T> runCompute<T>(
    Future<T> Function() computation, {
    Duration? timeout,
  }) async {
    final completer = Completer<T>();
    final timer = timeout == null
        ? null
        : Timer(timeout, () {
            if (!completer.isCompleted) {
              completer.completeError(TimeoutException('Computation timed out'));
            }
          });

    try {
      final result = await computation();
      if (!completer.isCompleted) {
        completer.complete(result);
      }
    } catch (e) {
      if (!completer.isCompleted) {
        completer.completeError(e);
      }
    } finally {
      timer?.cancel();
    }

    return completer.future;
  }
}

2. 资源管理

dart
class IsolatePool {
  final List<Isolate> _isolates = [];
  final int maxIsolates;

  IsolatePool(this.maxIsolates);

  Future<void> initialize() async {
    for (int i = 0; i < maxIsolates; i++) {
      final isolate = await Isolate.spawn(
        isolateFunction,
        'Isolate-$i',
      );
      _isolates.add(isolate);
    }
  }

  void dispose() {
    for (final isolate in _isolates) {
      isolate.kill();
    }
    _isolates.clear();
  }
}

3. 错误处理

dart
class IsolateErrorHandler {
  static Future<T> runWithErrorHandling<T>(
    Future<T> Function() computation,
  ) async {
    try {
      return await computation();
    } on IsolateSpawnException catch (e) {
      print('Isolate 创建失败: $e');
      rethrow;
    } catch (e) {
      print('执行错误: $e');
      rethrow;
    }
  }
}

常见问题和解决方案

1. 内存管理

  • 及时释放 Isolate 资源
  • 控制并发数量
  • 避免内存泄漏

2. 性能调优

  • 选择适当的并发级别
  • 合理分配任务
  • 监控性能指标

3. 调试技巧

  • 使用日志跟踪
  • 性能分析工具
  • 异常监控

总结

Flutter 的多线程和并发编程是构建高性能应用的重要工具:

  1. Isolate 提供了真正的多线程支持
  2. 异步编程让应用更加响应
  3. 并发控制确保资源合理使用
  4. 良好的实践提升应用质量

通过合理使用这些技术,我们可以构建出性能更好、响应更快的 Flutter 应用。