Dart的异步真的是异步的吗? [英] Is Dart's async really async?

查看:86
本文介绍了Dart的异步真的是异步的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码

  import'dart:async';var total = 0;最终迭代= 10000000;未来add()异步{print('开始添加-> $ total');for(var i = 0; i< erations; i ++){总数+ = 1;}print('end add-> $ total');}未来sub()异步{print('开始子-> $ total');for(var i = 0; i< erations; i ++){总计-= 1;}print('end sub-> $ total');}void main(List< String> args){添加();sub();打印('完成');} 

产生以下输出

 完成开始添加->0结尾添加->10000000开始子->10000000结束子->0 

我想知道 async 调用是否真的以异步方式执行.

看起来所有 async 方法都在第二个线程中运行,该线程始终是相同的,因此它们对于调用者而言是异步的,但彼此之间是不同步的.

我是对的吗?如果是这样,我该如何以真正异步的方式执行多个功能(就像创建新线程一样)?

修改

我并不是说它必须是平行的.它可以是异步的,而不是并行的.例如,一个1核处理器每个周期执行1条指令,但这并不意味着这些进程是一个接一个地执行的.处理器插入所有进程并同时执行它们,而不是并行执行.具有GIA的VM也具有此行为.

这不是我在Dart VM上看到的行为.当异步函数被调用时,它是同时执行给它的调用者的,而不是另一个异步函数.我期望异步功能可以并发到所有事物,而不仅仅是它的调用者.

我希望现在已经清楚了.

解决方案

这是异步的工作方式.只有一个线程和一个事件队列将任务排入队列.完成上一个同步执行后,将执行队列中的下一个任务.

如果需要并行执行,则可以利用隔离.

另请参见

更新

Dart中的代码(以及当转换为JS时)是单线程执行的.一次只运行一个执行线程.当要执行任务队列中的任务时,该任务将不间断地执行,直到完成为止.如果此代码调用异步操作,则这些异步调用将添加到队列中,并在到期时执行.

详细来说,它有点复杂,因为除了事件队列之外,还有微任务.在处理任务队列中的下一个任务之前先处理微任务,但这并不能改变总是一个任务在另一个任务之后执行而不会中断的情况.

这就是为什么不要在UI线程中长时间运行计算很重要的原因,因为这会使UI无响应.

Given the following code

import 'dart:async';

var total = 0;
final iterations = 10000000;

Future add() async {
  print('starting add -> $total');

  for (var i = 0; i < iterations; i++) {
    total += 1;
  }

  print('ending add -> $total');
}

Future sub() async {
  print('starting sub -> $total');

  for (var i = 0; i < iterations; i++) {
    total -= 1;
  }

  print('ending sub -> $total');
}

void main(List<String> args) {
  add();
  sub();
  print('done');
}

which generates the following output

done
starting add -> 0
ending add -> 10000000
starting sub -> 10000000
ending sub -> 0

I wonder if async calls are really executed in an async fashion.

It looks like all async methods run in a second thread which is always the same, so they are async for their callers, but not for each other.

Am I right? If so, how can I execute multiple functions in an really async fashion (just like creating new threads)?

edit

I am not saying it has to be parallel. It can be async without being parallel. For example, an 1-core processor executes 1 instruction per cycle, but it does not mean that the processes are executed one by one. The processer intercales all processes and executes them concurrently, not in parallel. A VM with GIA also has this behavior.

This is not the behavior I am seeing here on Dart VM. When an async function is called, it is executed concurrently to its caller, but not to another async functions. I was expecting that an async function was concurrent to everything, not just to its caller.

I hope it is clear now.

解决方案

This is how async works. There is only one thread and an event queue where tasks are enqueued. When the previous sync execution is completed, then the next task in the queue is executed.

If you need parallel execution you can leverage isolates.

See also

update

Code in Dart (and when transpiled to JS) is executed single-threaded. Only one thread of execution is run at one time. When a task from the task queue is due to be executed, it is executed without interruption until it is completed. If this code calls async operations these async calls are added to the queue and executed when they are due.

In details it's a bit more complicated because there are also microtasks in addition to the event queue. Microtasks are processed before the next task from the task queue is processed, but this doesn't change that always one task is executed after the other without interruption.

This is the reason why it is important to not do long running calculations in the UI thread because this makes the UI non-responsive.

这篇关于Dart的异步真的是异步的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆