Scala futures: what is the main thread expected during the execution of futures?

I (newbie) are testing my concepts about Scala futures and the right patterns for using them.

Premise
Scala futures are blocks of code that must execute asynchronously. Thus, the main thread creates one or more of these futures, sets onSuccess () [note: equally applicable to OnComplete / onFailure] callbacks and revenues. Callbacks are executed when futures complete their runs.

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

Whatever discussion / blog / API I have visited so far, the fact that the main thread should not wait mentions it: it can continue to do its job, allowing you to execute futures in parallel and be ready for the result.

Question

But my question is: in the simplest case, when he has finished doing what he is doing, the main thread will have to wait for the callbacks to complete, right? And since the interrupt mechanism is not provided, which may indicate that the future is complete, 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 that uses futures, waiting is ultimately inevitable, isn't it?

Note
I understand that we can link futures using combinators or a promise (and there are other patterns) to avoid this problem altogether. But I'm trying to clarify my concept that in certain cases, the use of futures does not eliminate the need to wait for their completion.

Is this too basic a question? Does this show a big void in my understanding?

+3
source share
1 answer

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

Lock

Computing a lock is very similar to a while .

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

This is a synchronous lock calculation. Thread cannot do anything until the lock action is complete, because it constantly checks the action.

You actually have as many threads as there are processors on the machine. We hope you can see how quickly they can be fully occupied, and a giant FIFO queue of "things to do."

Non blocking

In fact, the concept is very simple. Instead of constantly checking that something has been done, the check is performed at specified time intervals. Future will be checked for completion every 100 ms (say).

The amazing thing is that during each of these 100 ms breaks, Thread is free to do something else. That is why you get superior performance from async things.

Bottom row

It is physically possible for processors to do more things in a given amount of time. This is a very simple syllogism.

Say that you and your friend decided to meet for coffee at 3 pm. He does not appear.

You can sit in a cafe cursing relentlessly, or you can go home and bake cookies. While the cookie is baking, you check your phone every 5 minutes, until it finally texts, and then you meet.

In Scenario 1, you get angry and haven’t done much all day.

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

+3
source

All Articles