Scala期货:执行期货时,预期的主要线程是什么? [英] Scala futures: what is the main thread expected to do while futures are executing?

查看:55
本文介绍了Scala期货:执行期货时,预期的主要线程是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我(新手)正在测试有关Scala期货的概念以及使用它们的正确模式.

I (a newbie) am testing my concepts about Scala's Futures and the right patterns to use them.

前提
Scala的期货是要异步执行的代码块.因此,主线程会创建一个或多个此类期货,并安装onSuccess()[注:同样适用于OnComplete/onFailure]回调并继续.回调在期货完成运行时执行.

The premise
Scala's futures are blocks of code to be executed asynchronously. So, the main thread creates one or more such futures, installs onSuccess() [note: equally applicable to OnComplete/onFailure] callbacks and proceeds. The callbacks are executed as and when the futures complete their runs.

大概,这些回调会生成应由主线程使用的结果.结果存储在Try [T]容器中,并具有一次写入/多次读取约束.主线程(或其他线程,但这不是重点)决定何时在容器中达到峰值并收集结果以进行进一步处理.

Presumably, these callbacks generate results which are supposed to be used by the main thread. The results are stored in a Try[T] container, with a write-once/read-many constraint. The main (or any other thread but that is not the point) decide when to peak in the container and collect the results for further processing.

到目前为止,我一直在讨论/博客/API,提到主线程不必等待的事实:它可以继续做自己的事情,允许并行执行期货准备好结果.

Whichever discussion/blog/API I have laid my eyes on so far, mentions the fact that main thread doesn't have to wait: it can carry on doing its own thing, allowing the futures to be executed in parallel and be ready with the result.

问题

但是我的问题是:在最简单的情况下,它已经完成了正在做的事情,主线程将必须等待以使回调完成,不是吗?并且,由于没有提供可以表明未来已经完成执行的中断机制,因此主线程别无选择,只能等待"(可能有时间限制)以使结果变为准备好?换句话说,在使用期货的应用程序中,等待是最终不可避免的,不是吗?

But my question is: in the simplest case, where it has finished doing whatever it is doing, the main thread will have to wait for the callbacks to finish, will it not? And, because there is no interruption mechanism provided which can indicate that the future has finished execution, the main thread has no option but to 'wait' (possibly with a time-limit) for the result to be ready? In other words, in an application, where futures are employed, a wait is ultimately unavoidable, isn't it?

注意
我知道我们可以使用组合器或Promise(还有其他模式)将期货链接起来,从而完全避免出现此问题.但是,我试图澄清一下我的概念,即在某些情况下,使用期货并不能消除等待期货结束的需要.

Note
I understand that we can chain the futures, using combinators or Promise (and there are other patterns too), to avoid this issue altogether. But, I am trying to clarify my concept that under certain cases, using futures doesn't obviate the need to wait for them to finish.

这太基本了吗?在我的理解中它显示出很大的空白吗?

Is it too elementary a question? Is it showing a big void in my understanding?

推荐答案

了解Awaitasync之间的理论差异将很有用.这些分别称为阻塞和非阻塞.

It would be useful to understand the theoretical difference between Await and async. These are known as blocking and non-blocking respectively.

阻止

阻塞计算很像while循环.

while(!done) {
  if (isDone) {
    // do whatever
    done = true
  }
}

这是同步块计算. Thread在阻止动作完成之前无法做任何其他事情,因为它会不断检查该动作.

This is synchronous blocking computation. The Thread can't do anything else until the blocking action is complete as its constantly checking for the action.

您实际上只拥有与计算机上的处理器一样多的线程.希望您能看到如何迅速将它们完全占用,并形成一个庞大的要做的事情" FIFO队列.

You only really have as many threads as processors on the machine. Hopefully you can see how they can quickly be fully occupied and a giant FIFO queue of "things to do" is formed.

无阻塞

从本质上讲,这个概念非常简单.而不是连续检查是否已完成某件事,而是以给定的时间间隔进行检查.每100毫秒检查一次Future的完成情况.

In essence, the concept is very simple. Instead of continuously checking if something is done, the check is done at given time intervals. The Future will be checked for completion every 100ms(let`s say).

很棒的事情是,在这100ms的每个中断中,线程都可以自由地执行其他操作.这就是为什么您从async事物中获得卓越性能的原因.

The awesome thing is during every one of those 100ms breaks, the Thread is free to do something else. That's why you get superior performance from async things.

底线

在给定的时间间隔内,处理器在物理上有可能做更多的事情.这是一个非常简单的三段论.

It's physically possible for the processors to do more things in a given time interval. It's a very simple syllogism.

假设您和您的朋友安排在下午3点见面喝咖啡.他没有露面.

Say you and your friend arrange to meet for coffee at 3PM. He doesn't show up.

您可以坐在咖啡店里不停地咒骂,也可以回家烘烤饼干. Cookie烘烤时,您每5分钟检查一次电话,直到他最终发短信,然后见面.

You can either sit in the coffee shop relentlessly cursing or you can go home and bake cookies. While the cookies are baking, you check your phone every 5 minutes until he eventually texts and then you meet up.

在方案1中,您很生气,整天没有做很多事.

In scenario 1, you are angry and haven't done much all day.

在方案2中,您为自己的烹饪成功而感到高兴,并且很高兴见到朋友.

In scenario 2, you are delighted by your culinary success and in a great mood to see a friend.

这篇关于Scala期货:执行期货时,预期的主要线程是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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