/
isolate_page.dart
172 lines (159 loc) · 5.49 KB
/
isolate_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:isolate';
class IsolatePage extends StatefulWidget {
@override
State<StatefulWidget> createState() => _IsolatePageState();
}
class _IsolatePageState extends State<IsolatePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Future Page")),
body: Container(
margin: EdgeInsets.symmetric(horizontal: 4),
child: ListView(children: <Widget>[
Row(children: <Widget>[
_itemBtn(
'spawn Isolate', 1, Colors.pinkAccent.withOpacity(0.4)),
SizedBox(width: 4),
_itemBtn(
'spawnUri Isolate', 2, Colors.purpleAccent.withOpacity(0.4))
]),
Row(children: <Widget>[
_itemBtn('单向通讯', 3, Colors.blueAccent.withOpacity(0.4)),
SizedBox(width: 4),
_itemBtn('双向通讯', 4, Colors.blueGrey.withOpacity(0.4))
]),
Row(children: <Widget>[
_itemBtn('compute()', 5, Colors.orange.withOpacity(0.4)),
SizedBox(width: 4),
_itemBtn(
'compute() 02', 6, Colors.deepOrangeAccent.withOpacity(0.4))
]),
Row(children: <Widget>[
_itemBtn('compute() Error', 7, Colors.pink.withOpacity(0.4)),
SizedBox(width: 4),
_itemBtn('', 8, Colors.pinkAccent.withOpacity(0.4))
])
])));
}
_itemBtn(text, index, color) {
return Expanded(
child: FlatButton(
onPressed: () => _itemClick(index),
child: Center(
child: Text(text,
style: TextStyle(color: Colors.white, fontSize: 16.0))),
color: color));
}
_itemClick(index) async {
print('Current Button $index click --> start');
switch (index) {
case 1:
await _loadIsolateDate01();
break;
case 2:
await _loadIsolateDate03();
break;
case 3:
await _loadIsolateDate01();
break;
case 4:
await _loadIsolateDate02();
break;
case 5:
await _loadIsolateDate04();
break;
case 6:
await _loadIsolateDate05(false);
break;
case 7:
await _loadIsolateDate05(true);
break;
}
print('Current Button $index click --> end');
}
_loadIsolateDate01() async {
ReceivePort receivePort = ReceivePort();
await Isolate.spawn(_backgroundWork, receivePort.sendPort);
receivePort.listen((val) => print('listen -> 【$val】'));
}
static _backgroundWork(SendPort sendPort) async {
sendPort.send(
'BackgroundWork -> currentTime -> ${DateTime.now().millisecondsSinceEpoch}');
Future.delayed(Duration(seconds: 3), () {
return sendPort.send(
'BackgroundWork delayed 3s -> currentTime -> ${DateTime.now().millisecondsSinceEpoch}');
});
}
_loadIsolateDate02() async {
ReceivePort receivePort = ReceivePort();
var sendPort;
await Isolate.spawn(_backgroundWork2, receivePort.sendPort);
receivePort.listen((val) {
if (val is SendPort) {
sendPort = val as SendPort;
print("双向通讯建立成功");
}
print("Isolate Recevie Data -> 【$val】");
if (sendPort != null) {
sendPort.send(
'_loadIsolateDate02 -> currentTime -> ${DateTime.now().millisecondsSinceEpoch}');
}
});
}
static _backgroundWork2(SendPort sendPort) async {
ReceivePort receivePort = new ReceivePort();
receivePort.listen((val) {
print("Background Isolate Receive Data -> 【$val]");
});
sendPort.send(
'BackgroundWork -> currentTime -> ${DateTime.now().millisecondsSinceEpoch}');
sendPort.send(receivePort.sendPort);
Future.delayed(Duration(seconds: 2), () {
return sendPort.send(
'BackgroundWork delayed 2s -> currentTime -> ${DateTime.now().millisecondsSinceEpoch}');
});
Future.delayed(Duration(seconds: 5), () {
return sendPort.send(receivePort.sendPort);
});
}
_loadIsolateDate03() async {
ReceivePort receivePort = ReceivePort();
Isolate isolate = await Isolate.spawnUri(
new Uri(path: "package:flutter_app04/utils/second_isolate.dart"),
['params01, params02,params03'],
receivePort.sendPort);
receivePort.listen((val) => print('listen -> 【$val】'));
// isolate.kill(priority: Isolate.immediate);
}
_loadIsolateDate04() async {
print('main Isolate, current Isolate = ${Isolate.current.hashCode}');
print(await compute(getName, ''));
}
static String getName(String name) {
print('new Isolate, current Isolate = ${Isolate.current.hashCode}');
sleep(Duration(seconds: 2));
return '阿策小和尚';
}
_loadIsolateDate05(bool isError) async {
print('main Isolate, current Isolate = ${Isolate.current.hashCode}');
try {
print(await compute(_backgroundWork3, isError));
} catch (e) {
print(e);
}
}
static _backgroundWork3(bool isError) async {
print('new Isolate, current Isolate = ${Isolate.current.hashCode}');
if (!isError) {
return await Future.delayed(Duration(seconds: 2), () {
return 'BackgroundWork delayed 2s -> currentTime -> ${DateTime.now().millisecondsSinceEpoch}';
});
} else {
return await Future.error(ArgumentError.notNull('Input'));
}
}
}