
Understand it
A thread costs a stack and a scheduler slot, which is fine for a few hundred and painful for a hundred thousand. Async tasks are state machines sharing a small pool of threads, so waiting on ten thousand sockets is cheap — but a task that computes without yielding blocks everything sharing its thread. The dividing line is whether the work waits or works.
A thread is an operating-system execution context; an async task is a future that an executor polls. The async task can yield during nonblocking waits, allowing one thread to serve many waiting operations. A thread can execute CPU work in parallel with other threads when cores are available. These distinctions are more useful than saying one technology is for all I/O and the other for all CPU. Small systems may use threads for I/O perfectly well, and async applications still use threads underneath.
Imagine ten thousand mostly idle connections. One thread per connection can consume considerable stack reservation and scheduling overhead; an async runtime can represent each wait as compact task state and wake only ready work. Now imagine image compression that runs for a second without yielding. Put it directly on an executor worker and other tasks sharing that worker wait. Offload CPU-heavy or blocking work to an appropriate pool, with a bound on how much can queue.
For an interview, ask what the workload does while it is not producing results. If it waits on sockets, choose an async I/O model or another scalable event-driven design. If it computes, decide how many cores and jobs are available and use threads or a compute pool. Add cancellation and shutdown to the answer: a detached task can outlive a request, while a thread needs a join or explicit lifecycle. Measure latency and memory under representative load rather than repeating one universal task-count threshold.
Interview follow-up: How would the design change for mostly idle sockets? Name what remains bounded in both designs.
Read the code
async fn fetch(id: u32) -> Result<Body, Error> {
let response = client.get(id).await?;
Ok(response.body().await?)
}Async API sketch: the application supplies Body, Error and client; runtime and I/O support depend on the target.
Practice starter
// 500 images; bounded RAM; 8 available CPU cores.
// Each resize performs a long calculation without yielding.Try it
Decide between threads and async for two workloads and justify each.
What success looks like: You separate waiting from computing rather than picking by fashion.
Check your understanding
Which execution plan fits sustained CPU-bound image resizing across available cores?
Reveal the answer
- Spawn 500 async tasks and rely on await to parallelize each calculation.
- Use bounded compute parallelism and queue admission sized for the workload.
- Create one operating-system thread per image regardless of memory limits.
An async runtime can use multiple threads, but spawning tasks alone neither bounds CPU work nor makes a synchronous loop yield.
Go deeper
Common misconception: Create one operating-system thread per image regardless of memory limits.
With 8 cores, 500 threads add memory and scheduling cost without adding parallelism. Bound the workers to the available cores and queue the rest within the RAM limit.
- Identify the contract: Async makes waiting cheap and threads run work in parallel; neither owns all I/O or all CPU.
- Trace the concrete example: Ten thousand of these is routine; ten thousand threads is not.
- Test the changed constraint: How would the design change for mostly idle sockets? Name what remains bounded in both designs.
Transfer challenge: How would the design change for mostly idle sockets? Name what remains bounded in both designs.
Connections
- Channels vs Mutex: Moving ownership or sharing it
- Blocking an Executor: Why one sync call stalls a runtime
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Async vs Threads (courseId "interview", lessonId "q-async-vs-threads") Link: https://crabrix.com/learn/interview/q-async-vs-threads/ Goal: Choose concurrency and parallelism from the workload; async tasks still execute on threads. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-async-vs-threads) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.