a_thread_pool 1.1.5

  • Readme
  • Changelog
  • Example
  • Installing
  • new60

thread pool #

Help developers use Isolate more friendly under the flutter framework to activate the multi-core performance of the device.

Getting Started #

example: #

    //Run testIsolateRun in the isolated thread pool
    ThreadPool.io.run(testThreadRun, "params for testThreadRun");
    //Run testIsolateRun in the isolated thread pool with custom params
    final response = await ThreadPool.io.run(testStaticThreadRun, _AnyParam(true, 200, 200.0,"stringParam", {"kev1":0, "key2":"any type"}, ["fasfa", 1000]))
        .catchError((error){
      //catch exception from isolate thread
    });
    print(response);
    
    //catch AException from isolate thread
    await ThreadPool.io.run(testExceptionStaticThreadRun, "exception test").catchError((err){
      print("exception from thread:${err.runtimeType}, $err");
    });
    
    //catch any exception from isolate thread
    await ThreadPool.io.delay(Duration(seconds: 3), testLambdaExceptionStaticThreadRun, "exception test").catchError((err){
      print("exception from thread:${err.runtimeType}, $err");
    });

    //define a static function
    static _AnyResponse testStaticThreadRun(Object any) {
        ThreadPool.logger(LOG_LEVEL.INFO, "testIsolateRun", "working on thread ${Isolate.current}, param:$any");
        return _AnyResponse(false, 100, 300.0,"stringResponse", {"kev1Response":0, "key2Response":"any type"}, ["Response:fasfa", 2000]);
    }
    
    static _AnyResponse testExceptionStaticThreadRun(Object any) {
        ThreadPool.logger(LOG_LEVEL.INFO, "testExceptionStaticThreadRun", "working on thread ${Isolate.current}, param:$any");
        throw MyException("my excption from isolate, param:$any");
    }
    
    static _AnyResponse testLambdaExceptionStaticThreadRun(Object any) {
        ThreadPool.logger(LOG_LEVEL.INFO, "testLambdaExceptionStaticThreadRun", "working on thread ${Isolate.current}, param:$any");
        throw MyLambdaException("my lambda excption from isolate, param:$any");
    }


//define a top-level function
bool testThreadRun(Object any) {
  ThreadPool.logger(LOG_LEVEL.INFO, "testIsolateRun", "working on thread ${Isolate.current.toString()}, param:$any");
  return true;
}


class _AnyParam {
  final bool boolParam;
  final int intParam;
  final double doubleParam;
  final String stringParam;
  final Map<String, dynamic> mapParam;
  final List<dynamic> listParam;

  _AnyParam(this.boolParam, this.intParam, this.doubleParam, this.stringParam, this.mapParam, this.listParam);

  @override
  String toString() {
    return "bool:$boolParam, int:$intParam, double:$doubleParam, string:$stringParam, map:$mapParam, list:$listParam";
  }
}

class _AnyResponse {
  final bool boolParam;
  final int intParam;
  final double doubleParam;
  final String stringParam;
  final Map<String, dynamic> mapParam;
  final List<dynamic> listParam;

  _AnyResponse(this.boolParam, this.intParam, this.doubleParam, this.stringParam, this.mapParam, this.listParam);

  @override
  String toString() {
    return "response from thread, bool:$boolParam, int:$intParam, double:$doubleParam, string:$stringParam, map:$mapParam, list:$listParam";
  }
}

class MyException extends AException{
  MyException(String error) : super(error);
}

class MyLambdaException{
  ARunnable runnable = (param){};
  MyLambdaException(String error);
}

[1.1.5] - fix add library define #

[1.1.4] - fix export types #

[1.1.0] - support delay #

[1.0.9] - adjust README #

[1.0.8] - update demo #

[1.0.3] - init #

example/lib/main.dart

import 'dart:isolate';

import 'package:a_thread_pool/a_thread_pool.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ThreadPool Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Thread Pool Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    ThreadPool.logger = (level, tag, message) {
      print("level:$level $tag $message");
    };

    _runDemo();

    super.initState();
  }

  void _runDemo() async {
    //Run testIsolateRun in the isolated thread pool
    ThreadPool.io.run(testThreadRun, "params for testThreadRun");
    ThreadPool.addExceptionBuilder(MyExceptionBuilder());
    //Run testIsolateRun in the isolated thread pool with custom params
    final response = await ThreadPool.io
        .run(
            testStaticThreadRun,
            _AnyParam(true, 200, 200.0, "stringParam",
                {"kev1": 0, "key2": "any type"}, ["fasfa", 1000]))
        .catchError((error) {
      //catch exception from isolate thread
    });
    print(response);

    //catch AException from isolate thread
    await ThreadPool.io
        .run(testExceptionStaticThreadRun, "exception test")
        .catchError((err) {
      print("exception from thread:${err.runtimeType}, $err");
    });

    //catch any exception from isolate thread
    await ThreadPool.io
        .delay(Duration(seconds: 3), testLambdaExceptionStaticThreadRun,
            "exception test")
        .catchError((err) {
      print("exception from thread:${err.runtimeType}, $err");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            FlatButton(
              child: Text("Click Run Isolate Thread Pool Demo"),
              onPressed: () {
                _runDemo();
              },
            ),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  //define a static function
  static _AnyResponse testStaticThreadRun(Object any) {
    ThreadPool.logger(LOG_LEVEL.INFO, "testIsolateRun",
        "working on thread ${Isolate.current}, param:$any");
    return _AnyResponse(
        false,
        100,
        300.0,
        "stringResponse",
        {"kev1Response": 0, "key2Response": "any type"},
        ["Response:fasfa", 2000]);
  }

  static _AnyResponse testExceptionStaticThreadRun(Object any) {
    ThreadPool.logger(LOG_LEVEL.INFO, "testExceptionStaticThreadRun",
        "working on thread ${Isolate.current}, param:$any");
    throw MyException("my excption from isolate, param:$any");
  }

  static _AnyResponse testLambdaExceptionStaticThreadRun(Object any) {
    ThreadPool.logger(LOG_LEVEL.INFO, "testLambdaExceptionStaticThreadRun",
        "working on thread ${Isolate.current}, param:$any");
    throw MyLambdaException("my lambda excption from isolate, param:$any");
  }
}

//define a top-level function
bool testThreadRun(Object any) {
  ThreadPool.logger(LOG_LEVEL.INFO, "testIsolateRun",
      "working on thread ${Isolate.current.toString()}, param:$any");
  return true;
}

class _AnyParam {
  final bool boolParam;
  final int intParam;
  final double doubleParam;
  final String stringParam;
  final Map<String, dynamic> mapParam;
  final List<dynamic> listParam;

  _AnyParam(this.boolParam, this.intParam, this.doubleParam, this.stringParam,
      this.mapParam, this.listParam);

  @override
  String toString() {
    return "bool:$boolParam, int:$intParam, double:$doubleParam, string:$stringParam, map:$mapParam, list:$listParam";
  }
}

class _AnyResponse {
  final bool boolParam;
  final int intParam;
  final double doubleParam;
  final String stringParam;
  final Map<String, dynamic> mapParam;
  final List<dynamic> listParam;

  _AnyResponse(this.boolParam, this.intParam, this.doubleParam,
      this.stringParam, this.mapParam, this.listParam);

  @override
  String toString() {
    return "response from thread, bool:$boolParam, int:$intParam, double:$doubleParam, string:$stringParam, map:$mapParam, list:$listParam";
  }
}

class MyException extends AException {
  MyException(String error) : super(error);
}

class MyLambdaException {
  ARunnable runnable = (param) {};
  MyLambdaException(String error);
}

class MyExceptionBuilder extends DefaultAExceptionBuilder {
  @override
  AException build(anyException, stack) {
    return super.build(anyException, stack);
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  a_thread_pool: ^1.1.5

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:a_thread_pool/a_thread_pool.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
20
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
60
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • a_thread_pool that is a package requiring null.

Package not compatible with runtime flutter-web on web

Because:

  • package:a_thread_pool/a_thread_pool.dart that imports:
  • package:a_thread_pool/pool/thread_pool.dart that imports:
  • package:a_thread_pool/pool/thread_service.dart that imports:
  • dart:isolate

Health suggestions

Format lib/a_thread_pool.dart.

Run flutter format to format lib/a_thread_pool.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
dio ^3.0.9 3.0.9
flutter 0.0.0
stack_trace ^1.9.3 1.9.5
Transitive dependencies
charcode 1.1.3
collection 1.14.12 1.14.13
http_parser 3.1.4
meta 1.1.8 1.2.1
path 1.7.0
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test