IsolateTask class

IsolateTask:Dart多线程(Isolate)管理工具类

说明

便捷地在Flutter/Dart项目中管理多个Isolate,实现耗时任务分发与生命周期管理。

示例用法

// 1. 定义isolate入口函数
void isolateFunction(List<dynamic> args) {
  SendPort sendPort = args[0];
  dynamic taskData = args[1];

  // 执行耗时或计算密集型任务
  var result = performComputation(taskData);

  // 将结果发送回主isolate
  sendPort.send(result);
}

// 2. 主isolate中使用IsolateTask管理
void main() async {
  // 启动新isolate
  await IsolateTask.spawn('worker1', isolateFunction, '任务参数');

  // 检查isolate是否存在
  if (IsolateTask.exists('worker1')) {
    print('worker1 已启动');
  }

  // 结束指定isolate
  IsolateTask.kill('worker1');

  // 结束所有isolate
  IsolateTask.killAll();
}

Constructors

IsolateTask()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

activeCount int
当前活跃isolate的数量
no setter

Static Methods

exists(String name) bool
检查指定名称的isolate是否存在
kill(String name) → void
终止指定名称的isolate
killAll() → void
终止所有活跃的isolate
spawn(String name, void entryPoint(List), [dynamic message]) Future<void>
创建并启动一个新的isolate线程