-4

I have been reading about the concept but still it doesn't make sense to me. I want to clarify my question by giving an example.

First of all, if we have task1 and task2 which have to run in sequence, they call it synchronous. Whereas, if task2 runs before task1 finishes, it is called asynchronous. I came across such descriptions everywhere.

Where I am confused is that, if the tasks does not in order(one starts after the other finishes) how come things end up like we ordered them when we code line by line?

Imagine I write a code where task2 needs the final result of task1. In old way we would do it as:

var x = task1();

var y = task2(x);

So now above task2 needs x as an argument. But x can only return when the task1 finishes. Isn't it? So how come above task2 can start before task1 finishes? How can this work asynchronous way to make sense? Can someone clarify even though my definitions might not be correct?

3
  • 2
    Seems like a duplicate question. This might help. Commented Mar 26, 2021 at 1:31
  • My friend that's the surface knowledge with no example. I am confused at a very particular point. Commented Mar 26, 2021 at 1:35
  • @pnatk Linking to place where "they call it synchronous" may help others to reason about what you don't understand... Also consider specifying language/framework - precise meaning of "asynchronous" vary slightly between languages. (in this particular case the code shown is somewhat valid at least for JavaScript and C#). Also note that "synchronous" can mean two very different things in this context - "uninterrupted execution" and "execution in a sequence" - clarifying which meaning you use here would be nice too. Commented Mar 26, 2021 at 3:46

3 Answers 3

3

Your particular example has a data dependency: Task2 is dependent on Task1 to finish.

You can still get the benefits of async, depending on your language/framework.

One particular way is that Task1 can return a promise (aka a future): A token that says "this is the answer of Task1, and you can have it when it is ready". It just may not be ready yet. So you can pass that promise to Task2, and start Task2, and eventually when Task2 gets around to needing the value from Task1 it can ask the promise/future for its value. If Task1 is done: Then fine, Task2 gets the value right away. Otherwise it has to wait for it.

Other frameworks/libraries/languages might not have promises/futures, but they'll have something else, e.g., continuations (which are just callbacks, really), or special keywords, or whatever, to make the dependencies work out.

"async" is designed to make the most of your processing power when there's a lot of waiting going on for other things to happen (frequently: The other stuff is happening somewhere out there in the network, e.g., a database somewhere). It works without you having to write a lot of code to poll, or wait, or do threading stuff. But it works best if you can arrange either no data dependencies or a chain of data dependent tasks.

19
  • What can task2 start to do when it only gets a promise but not the actual returned value of x? Lets for simplicity say task2 is taking the square of its argument. Since x is not ready what will task2 start doing? Could you give an example? Maybe I simplified it too much but that's my level. Commented Mar 26, 2021 at 1:23
  • Well, you can read up on promises, but the basic idea is you ask it for the value, and if it is not ready yet (because Task1 isn't done) then Task2 waits. This is an automatic wait, arranged by the language/library/system. If Task2 needs the value immediately then there isn't much overlap in computation that can happen - but that's ok. Maybe you don't need the overlap. You just want it to wait until some database access or remote network call is done, and continue when it has the result without you having to do anything special to code that at all!. Commented Mar 26, 2021 at 1:27
  • (In other words, your example where task2 just takes the square of its argument: it's not a good use case for async. It's for when there's going to be waiting involved, and you don't want to write the code to do the waiting. You want the system to do the waiting and bookkeeping for you.) Commented Mar 26, 2021 at 1:28
  • Can you give an example which suits async case? A clear simple example. I still dont understand. Commented Mar 26, 2021 at 1:32
  • 2
    @pnatk "what is running task 1 then" is not your problem. Could be that task 1 is running on another processor, or another core in the same processor (effectively the same thing), or all the tasks could be running on a single core processor. Task 1 itself might be waiting on some other task, like a network request. In any case, there will be a scheduler that lets each task/thread/process get some time, so even on a single core machine many things can be in process all at the same time. This answer is a good one because it points out the role of data dependencies in ordering those tasks. Commented Mar 26, 2021 at 20:56
1

It’s really very simple. If task 2 needs the result of task 1 then it needs to wait for task 1 to finish and therefore task 1 and task 2 are not asynchronous to each other.

But if there is a third task that is independent, that can run asynchronous and in parallel to the others.

3
  • 1
    Its not that simple. see my comments Commented Mar 26, 2021 at 20:30
  • @pnatk It's exactly that simple. A task that needs results that don't exist yet has to wait for them, but if it can do other work while it waits it can go ahead and do that, and that's where the performance boost that async programming offers comes from. Commented Mar 26, 2021 at 20:57
  • ", but if it can do other work while it waits " what do you mean by "it"? is task "it" or program thread? what is task in your terminology? is it the thread? which thread? super super confusing Commented Mar 26, 2021 at 21:22
0

In the example given task2 has a dependency on the result from task1. In this case, task2 cannot start before the result from task1 is available. Because of this dependency, the two methods cannot be run concurrently (at the same time).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.