1 创建Isolate

  1. void test() {
  2. print('外部代码1');
  3. Isolate.spawn(func1, 10);
  4. print('外部代码2');
  5. }
  6. void func1(int count) {
  7. print("子线程");
  8. }
  9. main() {
  10. test();
  11. // dioDemo();
  12. }

执行结果:

  1. 外部代码1
  2. 外部代码2
  3. 子线程

2 Isolate子线程的执行顺序:Isolate创建后,和main线程同步执行

  1. void test() {
  2. print('外部代码1');
  3. Isolate.spawn(func1, 10);
  4. Isolate.spawn(func3, 10);
  5. Isolate.spawn(func1, 10);
  6. Isolate.spawn(func3, 10);
  7. Isolate.spawn(func1, 10);
  8. Isolate.spawn(func3, 10);
  9. Isolate.spawn(func1, 10);
  10. Isolate.spawn(func3, 10);
  11. print('外部代码2');
  12. }
  13. void func1(int count) {
  14. print("子线程1");
  15. }
  16. void func3(int count) {
  17. print("子线程2");
  18. }

执行结果:

  1. 外部代码1
  2. 外部代码2
  3. 子线程1
  4. 子线程2
  5. 子线程2
  6. 子线程1
  7. 子线程1
  8. 子线程2
  9. 子线程1
  10. 子线程2

3 Isolate线程内存空间独立

有独立的内存,不用担心资源抢夺的问题,不需要锁,但不利于数据交互

  1. void test() {
  2. print('外部代码1');
  3. Isolate.spawn(func1, 100);
  4. print("a=$a");
  5. print('外部代码2');
  6. }
  7. int a = 10;
  8. void func1(int count) {
  9. a = count;
  10. print("子线程1:$a");
  11. }
  12. void main() {
  13. test();
  14. // dioDemo();
  15. }

执行结果

  1. 外部代码1
  2. 子线程1:100
  3. a=10
  4. 外部代码2

4 Isolate线程间通信

  1. void test1()async{
  2. print("外部代码1");
  3. //创建Port
  4. ReceivePort port = ReceivePort();
  5. Isolate iso = await Isolate.spawn(func, port.sendPort);
  6. port.listen((message) {
  7. a = message;
  8. print(a);
  9. port.close();
  10. iso.kill();
  11. });
  12. print('外部代码2');
  13. }
  14. int a = 10;
  15. void func(SendPort send){
  16. print('第一个来了!');
  17. send.send(1000);
  18. }

打印结果:

  1. 外部代码1
  2. 第一个来了!
  3. 外部代码2
  4. 1000

5 compute的使用(在Isolate上层进行的封装)

  1. import 'package:flutter/foundation.dart';
  2. void computeTest(){
  3. print('外部代码1');
  4. compute(func2,10).then((value) => print(value));
  5. print('外部代码2');
  6. }
  7. int func2(int count){
  8. sleep(Duration(seconds: 1));
  9. return 10000;
  10. }

打印结果:

  1. 外部代码1
  2. 外部代码2
  3. 10000

await 等待

  1. void computeTest() async{
  2. print('外部代码1');
  3. int x = await compute(func2,10);
  4. print(x);
  5. sleep(Duration(seconds: 1));
  6. print('外部代码2');
  7. }
  8. int func2(int count){
  9. return 10000;
  10. }

打印结果

  1. 外部代码1
  2. 10000
  3. 外部代码2

多线程执行

  1. void isolateDemo(){
  2. Future(()=>compute(func1,123).then((value) => print("1结束了")));
  3. Future(()=>compute(func1,123).then((value) => print("2结束了")));
  4. Future(()=>compute(func1,123).then((value) => print("3结束了")));
  5. Future(()=>compute(func1,123).then((value) => print("4结束了")));
  6. Future(()=>compute(func1,123).then((value) => print("5结束了")));
  7. }
  8. void func1(int count){
  9. }
  10. /*
  11. //多个线程结束后回到主线程的时间没有固定顺序,每次运行打印顺序可能不一样:
  12. 5结束了
  13. 1结束了
  14. 4结束了
  15. 3结束了
  16. 2结束了
  17. */

案例

  1. void isolateDemo(){
  2. loadData().then((value) => print("1结束了"));
  3. loadData().then((value) => print("2结束了"));
  4. loadData().then((value) => print("3结束了"));
  5. loadData().then((value) => print("4结束了"));
  6. loadData().then((value) => print("5结束了"));
  7. }
  8. Future loadData(){
  9. return Future((){
  10. compute(func1,123);
  11. });
  12. }
  13. /*
  14. //loadData().then监听的是主线程的Future,打印有序
  15. I/flutter (23211): 1结束了
  16. I/flutter (23211): 2结束了
  17. I/flutter (23211): 3结束了
  18. I/flutter (23211): 4结束了
  19. I/flutter (23211): 5结束了
  20. */
  21. void isolateDemo1(){
  22. loadData1().then((value) => print("1结束了"));
  23. loadData1().then((value) => print("2结束了"));
  24. loadData1().then((value) => print("3结束了"));
  25. loadData1().then((value) => print("4结束了"));
  26. loadData1().then((value) => print("5结束了"));
  27. }
  28. Future loadData1(){
  29. return Future((){
  30. return compute(func1,123);
  31. });
  32. }
  33. /*
  34. //loadData().then监听的是compute子线程的Future的返回,多个子线程间是并行执行的,打印无序
  35. //但是then中执行的代码打印输出是在主线程中执行
  36. I/flutter (23211): 1结束了
  37. I/flutter (23211): 5结束了
  38. I/flutter (23211): 3结束了
  39. I/flutter (23211): 4结束了
  40. I/flutter (23211): 2结束了
  41. */

Dart多线程5.png

then中的代码块师傅添加到微任务:不会,then会和Future在同一个消息循环,是同一个任务,在Future任务执行完成后立刻执行 Future x = Future((){
print(‘A异步任务1’);
scheduleMicrotask(()=>print(“B微任务1”));
});
x.then((value) => print(‘C异步1结束’));
print(‘D主任务’);

打印:

I/flutter (23211): D主任务I/flutter (23211): A异步任务1 I/flutter (23211): C异步1结束 I/flutter (23211): B微任务1

6 Timer.run:开启异步任务,本质是开启Timer

  1. import 'dart:async';
  2. void test1(){
  3. Timer.run(() {
  4. print("异步任务");
  5. });
  6. print("来了");
  7. }
  8. Timer _timer;
  9. void test2(){
  10. int _count = 0;
  11. _timer = Timer.periodic(Duration(seconds: 1),(timer){
  12. print(Isolate.current.debugName);
  13. _count++;
  14. print(_count);
  15. if(_count == 99){
  16. timer.cancel();
  17. }
  18. });
  19. }
  20. @override
  21. void dispose(){
  22. super.dispose();
  23. if(_timer!=null && _timer.isActive){
  24. _timer.cancel();
  25. }
  26. }

注意: 在initState中创建的timer,需要在dispose中取消掉,否则重构页面时,会有多个timer