a_thread_pool 1.0.6 a_thread_pool: ^1.0.6 copied to clipboard
a isolate util, help developers use Isolate more friendly under the flutter framework to activate the multi-core performance of the device.
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> {
int _counter = 0;
@override
void initState() {
ThreadPool.logger = (level, tag, message){
print("level:$level $tag $message");
};
//Run testIsolateRun in the isolated thread pool
ThreadPool.io.run(testThreadRun, "params for testThreadRun");
//Run testIsolateRun in the isolated thread pool with custom params
ThreadPool.io.run(testStaticThreadRun, _AnyParam(true, 200, 200.0,"stringParam"))
.catchError((error){
//catch exception from isolate thread
});
super.initState();
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
//define a static function
static bool testStaticThreadRun(Object any) {
ThreadPool.logger(LOG_LEVEL.INFO, "testIsolateRun", "working on thread ${Isolate.current.toString()}, param:$any");
return true;
}
}
//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;
_AnyParam(this.boolParam, this.intParam, this.doubleParam, this.stringParam);
@override
String toString() {
return "bool:$boolParam, int:$intParam, double:$doubleParam, string:$stringParam";
}
}