What is technical the difference between a thread and an async-operation?

931 views

I assume that two threads are working on a single cpu like this:

| Thread1 | Thread2 |
|———|———|
| a1 | b1 |
| a2 | b2 |
| a3 | b3 |

And are batched like this on the CPU

a1-b1-a2-b2-a3-b3

In: Technology

4 Answers

Anonymous 0 Comments

A thread is when you ask the operating system to start running another part of the program at the same time. If there are more parts running than CPU cores, the operating system will switch between them. Threads have a bunch of features (like separate stacks) that make them relatively “expensive”. A program shouldn’t have thousands of threads because it will waste memory and time.

“Async tasks” depend on the programming language, but they’re generally things you can do in the background that are too short to be their own thread. The program will create one thread (or a few) and then that thread (or those threads) will do async tasks whenever they are ready to be done. This means a new thread doesn’t need to be created for every task.

“Async tasks” can also be things that don’t use a thread at all, as long as the program can remember it’s waiting for something to happen. For example, waiting for the user to type something could be an async task. The program won’t use a thread to wait for the user to type something (because that’s a waste of a thread) but it knows that when the user does type something, the task should be marked as completed.

“Async tasks” got a big boost in popularity some time ago because: people wanted to do more things asynchronously (in the background), people realised that having a thread for every single thing is not efficient, and because JavaScript basically forces you to use them so people got used to them.

You are viewing 1 out of 4 answers, click here to view all answers.