async_pool 0.2.1 copy "async_pool: ^0.2.1" to clipboard
async_pool: ^0.2.1 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
140
pub points
10%
popularity

Publisher

unverified uploader

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

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

More

Packages that depend on async_pool