dart 语言的异步 同步 构造器 隔离 元注解 注释方式bash
下面依次调用 三个方法 输出的顺序为1--3--4--2--5
await该关键字会执行暂停当前线程 等外部执行完毕再执行剩下的异步
main() async {
// async wait
getName1();
getName2();
getName3();
}
复制代码
/ async wait
Future<void> getName1() async {
// getStr1();//能够不用await打断点看下await后的区别
await getStr1(); //遇到第一个await表达式执行暂停,返回future对象,await表达式执行完成后继续执行
await getStr2(); //await表达式能够使用屡次
print('5');
}
getStr1() {
print('1');
}
getStr2() {
print('2');
}
getName2() {
print('3');
}
getName3() {
print('4');
}
复制代码
每次新建一个Future 会将这个Future添加到队列当中 先添加先执行 下面代码的输入顺序为f7--f1--f6--f3--f5--ff2--f4
async
void testFuture() {
Future f = new Future(() => print("f1"));
Future f1 = new Future(() => null); //7163524
// Future f1 = new Future.delayed(Duration(seconds: 1) ,() => null);//7132465
Future f2 = new Future(() => null);
Future f3 = new Future(() => null);
f3.then((_) => print("f2"));
f2.then((_) {
print("f3");
new Future(() => print("f4"));
f1.then((_) {
print("f5");
});
});
f1.then((m) {
print("f6");
});
print("f7");
}
复制代码
微队列会优先与队列执行,微队列执行会在当前正在执行的Future任务 执行完才会去执行微队列的任务, 若是then返回一个future任务那这个任务也会放到队列中, delayed延迟队列会在最后执行 下面的代码输出顺序为s9--s1--s8--s3--s4--s6--s5--s10--s7--s11--s12--s2
ide
void testScheduleMicrotask() {
//918346572
scheduleMicrotask(() => print('s1'));
new Future.delayed(new Duration(seconds: 1), () => print('s2'));
new Future(() => print('s3')).then((_) {
print('s4');
scheduleMicrotask(() => print('s5'));
}).then((_) => print('s6'));
new Future(() => print('s10'))
.then((_) => new Future(() => print('s11')))
.then((_) => print('s12'));
new Future(() => print('s7'));
scheduleMicrotask(() => print('s8'));
print('s9');
}
复制代码
全部的 Dart 代码在 isolates 中运行而不是线程。 每一个 isolate 都有本身的堆内存,而且确保每一个 isolate 的状态都不能被其余 isolate 访问。 其实 一个main方法就是一个隔离函数
main() async {
var receivePort = new ReceivePort();
await Isolate.spawn(echoo, receivePort.sendPort);
receivePort.listen((s) {
if (s is SendPort) {
s.send("不我想要桃子");
} else
print(s);
});
}
/// 新isolate的入口函数
echoo(SendPort sendPort) async {
// 实例化一个ReceivePort 打开接收端口以接收消息
var port = new ReceivePort();
// 把它的sendPort发送给宿主isolate,以便宿主能够给它发送消息
sendPort.send(port.sendPort);
sendPort.send("给你烂苹果");
// 监听循环接收消息
port.listen((s) {
print(s);
});
}
复制代码
sync* async用于生成 Iterrable 和 Stream yield yield 优化性能性能
返回值是Iterable 须要调用 moveNext() 才会执行迭代优化
//调用getSyncGenerator当即返回Iterable
var it = getSyncGenerator(5).iterator;
// 调用moveNext方法时getSyncGenerator才开始执行
while (it.moveNext()) {
print(it.current);
}
//同步生成器: 使用sync*,返回的是Iterable对象
Iterable<int> getSyncGenerator(int n) sync* {
print('start');
int k = n;
while (k > 0) {
//yield会返回moveNext为true,并等待 moveNext 指令
yield k--;
}
print('end');
}
复制代码
返回值是Stream 执行了只有执行了listen以后函数才会执行,也能够使用能够使用StreamSubscription对象对数据流进行控制ui
//调用getAsyncGenerator当即返回Stream,只有执行了listen,函数才会开始执行
// getAsyncGenerator(5).listen((value) => print(value));
StreamSubscription subscription = getAsyncGenerator(5).listen(null);
subscription.onData((value) {
print(value);
if (value >= 2) {
subscription.pause(); //能够使用StreamSubscription对象对数据流进行控制
}
});
//异步生成器: 使用async*,返回的是Stream对象
Stream<int> getAsyncGenerator(int n) async* {
print('start');
int k = 0;
while (k < n) {
//yield不用暂停,数据以流的方式一次性推送,经过StreamSubscription进行控制
yield k++;
}
print('end');
}
复制代码
//同步
var it1 = getSyncRecursiveGenerator(5).iterator;
while (it1.moveNext()) {
print(it1.current);
}
//异步
getAsyncRecursiveGenerator(5).listen((value) => print(value));
//递归生成器:使用yield*
Iterable<int> getSyncRecursiveGenerator(int n) sync* {
if (n > 0) {
yield n;
yield* getSyncRecursiveGenerator(n - 1);
}
}
//异步递归生成器
Stream<int> getAsyncRecursiveGenerator(int n) async* {
if (n > 0) {
yield n;
yield* getAsyncRecursiveGenerator(n - 1);
}
}
复制代码
class Todo {
final String who;
final String what;
const Todo({this.who, this.what});
}
复制代码
// 这是单行注释
复制代码
/*
* 这是多行注释
* 这是多行注释
*/
复制代码
两种方式
/// 这是文档注释
/**
* 这是文档注释
*/
复制代码