DevPrep

All topics › Multithreading & concurrency

Asynchrony vs multithreading — what's the difference?

Middle PRO

The essence

These are two independent characteristics.

Asynchrony is about execution order. A function does not return its result right away; it hands control back to the caller with a promise to report the result later (a callback, a Publisher, await). Nothing is said about where or on how many threads that result is computed. Asynchronous code lives happily on one thread: the main thread's run loop handles touches, timers and network callbacks one by one; JavaScript in the browser works the same way.

Multithreading is about physical execution. A process has several threads, each with its own stack, scheduled independently by the kernel; on a multi-core CPU they truly run in parallel. Threads share the process address space, which is where data races come from.

A useful pair of terms: concurrency is a program structure that lets tasks overlap in time; parallelism is simultaneous execution on several cores. Asynchrony gives concurrency; multithreading is a way to get parallelism.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store