0

As far what I understood is that, multi-threading is a capability we can provide to a single program to run multiple task or code blocks in parallel. For example, a spell checker is running on a separate thread while we are typing in a word doc. It is similar to multi tasking being done but on a single program level.

What asynchronous programming help us to achieve us is to provide the capability to write code in such a manner that it does not block the thread which is currently running the process and executes the rest of the code in a sequential manner. For example if we make a db call to get some data and then perform some operation on the received data. After that some other stuff which is independent is executing. So in order to not block the code flow we can make an async call to db using a call back performing the logical operation on data. And the rest of the code is executing sequentially.

My question is, if we know what dependent and independent task in our program are, can't we simply use concept of multi threading only? Why is this new concept of asynchronous programming is introduced? Like promises, call back in JavaScript or completable future in java. Also completable future was introduced in Java 8. What was being there before Java 8 then?

Please correct me if I am wrong any where in my statement.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Aki
  • 55
  • 1
  • 1
  • 8
  • It's not a new concept – GetSet Jul 13 '20 at 18:10
  • Async will still block, it uses something called cooperative multitasking, so if in a callback you created an infinite loop everything will hang. With threads it's working at the CPU/OS level, 1 thread doesn't block another thread, unless critical sections / locks are used. Threads also have way more complications, even incrementing a number is not trivial. – Keith Jul 13 '20 at 18:19
  • 1
    [What is the difference between concurrency, parallelism and asynchronous methods?](https://stackoverflow.com/questions/4844637/what-is-the-difference-between-concurrency-parallelism-and-asynchronous-methods) – akuzminykh Jul 13 '20 at 18:21

1 Answers1

1

Well, theoretically you could limit yourself to using async functions and Threads only, as far as I know.

Your question is actually about the whole programming languages' paradigm. My answer is that even though modern apps make frequent use of multi-threading, the majority of code is still executed sequentially. Input -> calculations -> output. That didn't change over the years.

Now, from more practical point of view. In Javascript, if you used only async functions, you'd find yourself in a callback hell in no time :) Pretty much the same result if you executed everything concurrently in Java.

And the final question from my side: where would you draw the line? You want to execute everything asynchronously, ok, but remember that these async blocks of code are still synchronous by itself. So what, are you asking about executing every line of code in parallel? If so, I think it's not a Java/JS question, but rather an unanswered question to recruit a group of new-programming-language-developers :)

That's an answer coming from a java/js dev, maybe there exist some languages that'd satisfy your questions, but I'm not aware of them.

k-wasilewski
  • 3,943
  • 4
  • 12
  • 29
  • Writing code in async way in order to not block the current thread i.e not waiting for some instruction to finish before moving to next one. Lets take an example where I made a DB query to get some data and do some operation on that data. After that there is a code for calculating factorial of a number. Say our code is running on a single thread then it's waiting for db call to finish in order to calculate factorial. But here we can can use multit-threading to do both task concurrently, but the thread which is executing db code is itself blocking. – Aki Jul 14 '20 at 04:08
  • Both method provide us the same advantage of writing non-blocking code but it is up to a developer what to use right ?. Thank you for the answer – Aki Jul 14 '20 at 04:09
  • Yes, it's developer's call whether to use async or not. – k-wasilewski Jul 14 '20 at 09:08