0

I'm not an English native speaker. The word synchronous and asynchronous have always been confusing to me while I learn about them in coding.

Linguistically, synchronous means: existing or occurring at the same time and asynchronous means: not existing or occurring at the same time

In Javascript, for example,

synchronous means: single-threaded and blocking. only one operation can be in progress at a time. code instructions run in sequence.

and

asynchronous means: running in parallel

how did that happen? asynchronous in programming holds the meaning of synchronous linguistically. am I missing something?

Sumaya A.
  • 1
  • 1
  • 1
    i just found a related question https://stackoverflow.com/questions/1596589/why-are-asynchronous-processes-not-called-synchronous – Sumaya A. Feb 25 '23 at 23:00
  • Think about it in terms of people queuing. If there are two or more queues (parallel), then people don't have to wait for people in the other queues (asynchronous). You always have to wait for people in your *own* queue though or if there *is* only one queue (that's synchronous). – Wyck Feb 25 '23 at 23:22
  • 1
    Super important: asynchronous does _not_ mean parallel. Asynchronous only means "not necessarily running tasks in the same order as written down". A task scheduler can run instructions asynchronously in a single thread just fine. – Mike 'Pomax' Kamermans Feb 25 '23 at 23:37
  • The word "synchronous" has [several definitions](https://www.dictionary.com/browse/synchronous). Besides the general definition with which you're familiar ("1. occurring at the same time"), it has a specialized definition in computing: "5. relating to or being a computer operation that must complete before another event can begin". – Michael Liu Feb 25 '23 at 23:55
  • See also https://stackoverflow.com/questions/67258642/whats-synchronous-about-javascript-synchronous-code – Bergi Feb 26 '23 at 00:15

2 Answers2

2

Aychronous in JavaScript means "running at a different time", with particular emphasis on not running in the same call out from the event loop's task manager. It does not usually mean "running in parallel" with the main thread1.

As an example, if a script fetches data from the network, the fetch operation takes time to perform and the initiating code does not wait for it to complete. Instead it will be notified of the result later, in a call back function, a promise handler or by using await in an async function.

Note async functions store their execution context after returning a promise to the calling code for the result or an await operation and return to the event loop (task manager) after adding then and catch handlers to the awaited promise which will restore the async functions execution context and resume execution.


1 Service workers come closest to running in parallel, possibly running in a different CPU core, but have tightly controlled data exchange mechanisms tp avoid simultaneous access to data by the main and worker threads.

traktor
  • 17,588
  • 4
  • 32
  • 53
1

Synchronous does not mean single-threaded, asynchronous does not mean parallel

Async - not schedule (no specifed start time) - run whenever he can

Sync - schedule (specifed start time - sequence) - after function not start if before not run

That means in any language...

Async can be one thread, concurrent or parallel

At the same time, the same program is playing on all TV sets, it has a fixed schedule, therefore it is synchronized. Asynchronously, he wouldn't have and wouldn't play at the same time(or played), that's why.

At the same time, everyone has lessons at school(have schedule).The schedule may be different, it could be in sequence, not all at once. Asynchronous can be all at once, sequence indifferently.

At the same time means a schedule and not that the functions are executed at the same time.

Therefore, the asynchronous function when it finds a free thread will execute without waiting for the previous one to finish and will not block the next one as long as there is a free thread(then it will be parallel ), provided the programming language or specific function supports threads, otherwise it will just be async

async: (3) (1) (2)

sync: (1) (2) (3)

parallel:

(1)

(3)

(2)

concurrent::

--(1)

------ (2)

-------------(3)

Sorry for my english i use translator

Pawells
  • 27
  • 8