IsolateTask class

Isolate线程管理工具类

使用示例:

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

  // 在这里处理耗时任务
  var result = heavyComputation(message);

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

// 2. 在主isolate中使用
void main() async {
  // 启动新的isolate
  await IsolateManager.spawn('worker', isolateFunction, data);

  // 检查isolate是否存在
  if(IsolateManager.exists('worker')) {
    print('Worker isolate is running');
  }

  // 完成后终止isolate
  IsolateManager.kill('worker');

  // 或终止所有isolate
  IsolateManager.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线程 name - isolate名称标识 entryPoint - isolate入口函数 message - 传递给isolate的消息