async_pool 0.1.0 icon indicating copy to clipboard operation
async_pool: ^0.1.0 copied to clipboard

A future and async thread pool project.has a syntax similar to CompletableFuture in java

example/lib/main.dart

import 'dart:async';
import 'dart:io';

import 'package:async_pool/async_pool.dart';

void main() async {
  print('future线程池测试');
  await futureTest();
  print('future线程池测试结束');
  print('isolate线程池测试');
  await isolateTest();
  print('isolate线程池测试结束');
}

/// future pool test
Future<void> futureTest() async {
  List<CompletableFuture> fs = List.generate(1000, (index) => index)
      .map((e) => CompletableFuture.runAsync(() async {
            await Future.delayed(Duration(milliseconds: 100));
            print('$e:${DateTime.now()} ');
            return e;
          }))
      .toList();
  await CompletableFuture.join(fs);
  print(fs.map((e) => e.result).length);
}

/// isolate pool test
Future<void> isolateTest() async {
  IsolateExecutor isolateExecutor = IsolateExecutor(
      maximumPoolSize: Platform.numberOfProcessors, keepActiveTime: 1);
  List<CompletableIsolate> list = List.generate(1000, (index) => index)
      .map((index) => CompletableIsolate.runAsync(test, index,
          isolateExecutor: isolateExecutor))
      .toList();
  // for (var value in list) {
  //   value.cancel();
  // }
  await CompletableIsolate.join(list);
  print(list.map((e) => e.result).where((element) => element != null).length);
}

Future<String> test(int index) async {
  await Future.delayed(Duration(milliseconds: 100));
  String p = '$index ${DateTime.now()}';
  print(p);
  return p;
}
0
likes
130
pub points
0%
popularity

Publisher

unverified uploader

A future and async thread pool project.has a syntax similar to CompletableFuture in java

Repository (GitHub)

Documentation

API reference

License

Icon for licenses.BSD-3-Clause (LICENSE)

More

Packages that depend on async_pool