13.Dart - 异步支持

Dart - 异步支持

Future发布延迟任务

void main() {
  /**
   * Futrue,未来.
   * 用来发布延迟任务
   * seconds 秒的意思
   */

  //发布延迟3秒的任务

  Future.delayed(Duration(seconds: 3)).then((i) {
    print(i);
  });
}

event-loop实现互相发消息

//

import 'dart:io';
import 'dart:isolate';

int i;

void main() {
  i = 10;

  //1.创建一个消息接收器
  ReceivePort receivePort = new ReceivePort();
  //2.创建isolate
  Isolate.spawn(isolateMain, receivePort.sendPort);

  //3.创建接受其他isolate的方法
  receivePort.listen((message) {
    if (message is SendPort) {
      message.send("好的");
    } else {
      print("接收到子isolate的消息" + message);
    }
  });

  //增加休眠,是会影响listen的时机
  sleep(Duration(seconds: 2));
  print("休眠成功");
}

//新isolate的入口函数
void isolateMain(SendPort sendPort) {
  //isolate是内存隔离的,i在这里面取不到
//  print(i);

  //1.创建一个消息接收器
  ReceivePort receivePort = new ReceivePort();
  //2.创建一个发送
  sendPort.send("我是你呀");
  receivePort.listen((msg) {
    print("a a a a a $msg");
  });
}

微任务microtask

//

import 'dart:io';
import 'dart:isolate';

int i;

void main() {
  i = 10;

  ReceivePort receivePort = new ReceivePort();
  receivePort.listen((msg) {
    print(msg);

    //微任务队列会不会插队?
    // 会
    //开启一个微任务队列
    Future.microtask(() {
      print("我是微任务队列!");
    });
  });

  //future 未来
  //先输出微任务队列,因为微任务队列比event高,无论有多少个event都会先执行微任务队列
  //在微任务队列里面 sleep,死循环,  后面event的都会接收不到了,因为是单线程
  Future.microtask(() {
    print("微任务队列1");
  });
  receivePort.sendPort.send("你好呀呀呀1");

  Future.microtask(() {
    print("微任务队列2");
  });
  receivePort.sendPort.send("你好呀呀呀2");

  Future.microtask(() {
    print("微任务队列3");
  });
  receivePort.sendPort.send("你好呀呀呀3");

  //休眠三秒
  sleep(Duration(seconds: 3));
}

async和await实现异步

printStringData() async {
  String news = await gatherNews();
  print(news);//输出获取到的新闻,1秒后输出
  String news1 = await gatherNews2();
  print(news1);//输出获取到的新闻,5秒后输出
}

main() {
  printStringData(); //打印出文本信息
  printWinningNumbers(); //打印出错误数字
}

printWinningNumbers() {
  print('随便输出的: 1,2,3');
}

const news = '你好,我是新闻'; //常量
final newsStream = new Stream.periodic(Duration(seconds: 1), (s) => news);
final newsStream2 = new Stream.periodic(Duration(seconds: 5), (s) => news);

Future gatherNews() => newsStream.first;
Future gatherNews2() => newsStream2.first;
/*
1. 开始程序执行
2. main函数调用printStringData,因为它被标记为async,所有在该函数任何代码被执行之前立即返回一个Future。
3. 剩下的打印执行。因为它们是同步的。所有只有当一个打印函数执行完成过后才能执行下一个打印函数。
4. 函数printStringData函数体开始执行
5. 在到达await之后,调用gatherNews,程序暂停,等待gatherNews返回的Future完成。
6. 当Future完成,printStringData继续执行,打印新闻。
7. 当printStringData执行完成过后,最开始的Future返回完成,程序退出。*/

//如果async函数没有明确指定返回值,返回的null值的Future

利用then实现回调接口

printNews() async{
  Future future = getNewsString();
  future.then((news)=>print(news));
}
void main(){
  printNews();
}
const news = "我是新闻";

Future getNewsString() => new Stream.periodic(Duration(seconds: 2),(_)=>news).first;

async异步方法

import 'dart:io';

void main(){

}

// 方法是异步的,
// 方法内部的本来是异步的方法,变成同步的
// 开发中使用频繁

//async表示是异步方法,await必须在async中使用
Future<String> readFile() async{
  //await 意思是 dengdaifuture执行完后再执行后面代码
  String s1 = await new  File(r"C:\Users\zhulianghao\Desktop\Dart异步编程.md").readAsString();
  String s2 = await new  File(r"C:\Users\zhulianghao\Desktop\Dart异步编程.md").readAsString();

  return "$s1$s2";
}
发布了49 篇原创文章 · 获赞 6 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/zlhyy666666/article/details/104625917