What Are Some Real-Life Examples of Multithreading?
Multithreading is the technique of running several independent instruction streams, called threads, inside one program at the same time. The threads share the program's memory, so they can pass data cheaply, and the operating system schedules them across the CPU cores. Real-life examples include a browser that keeps scrolling while a page loads, a web server that answers 200 requests at once, and a game that draws frames while it computes physics.
Each example below names the threads and the job each one does. That is the level of detail an interviewer asks for when the question is "give me a real example of multithreading".
Eight examples and what each thread does
| Program | Threads inside it | Why threads instead of one loop |
|---|---|---|
| Web browser | UI thread, network threads, one main thread and a compositor thread per page | A slow page must not freeze the address bar or other tabs |
| Web server (Tomcat, ASP.NET) | Acceptor thread plus a pool of worker threads, 200 by default in Tomcat | Each request waits on a database, and other requests can run during that wait |
| Video game | Render thread, simulation thread, audio thread, asset loading thread | A frame has about 16 ms at 60 frames per second, and no single core can do all the work in that time |
| IDE (IntelliJ, Visual Studio) | Editor thread, indexing thread, syntax check thread, build and test threads | Typing must stay instant while the project is re-indexed |
| Phone app | Main (UI) thread plus background threads for network and disk | Android throws an exception if a network call runs on the main thread |
| Video encoder (x264, HandBrake) | One thread per frame or per slice of a frame | Encoding is CPU-bound, so throughput scales with core count |
| Pi calculator (y-cruncher) | One thread per core running parts of a very large multiplication | Computing pi to trillions of digits takes days even with every core busy |
| Java virtual machine | Application threads plus garbage collector threads and a JIT compiler thread | Memory can be reclaimed and code compiled while the program keeps running |
Web browsers
Chrome gives each site its own process, and each process contains several threads. The main thread runs JavaScript, builds the page layout and handles clicks. The compositor thread scrolls and animates the already drawn layers, which is why a page still scrolls while its JavaScript is busy. Network threads fetch resources and hand the bytes back to the main thread.
The browser process that owns the window has its own UI thread and I/O thread. A crash in one tab's renderer therefore does not close the window.
Web servers
A traditional web server keeps a pool of worker threads and assigns one to each incoming request. Tomcat's default pool size is 200 threads. While one worker waits 50 ms for a database reply, the other 199 keep serving. Without threads the server would serve one request at a time and sit idle during every database call.
Not every server uses this model. Nginx and Node.js use one thread per core with an event loop, and PostgreSQL starts a separate process per connection instead of a thread. Both choices are answers to the same problem: keep the CPU busy while requests wait on I/O.
Video games
A game engine has a fixed budget of about 16 milliseconds per frame at 60 frames per second. One thread cannot run physics, animation, artificial intelligence, audio mixing and rendering inside that budget. Engines therefore run a simulation thread that advances the world and a render thread that draws the previous frame. Audio runs on its own thread because a gap in sound is more noticeable than a dropped frame. Asset streaming threads load textures from disk so that the player never sees a loading screen mid-level.
Editors and phone apps
An IDE keeps the editor on one thread and everything slow on others. When you type, a syntax check thread re-parses the file, an indexing thread updates symbol tables, and a test runner may execute the affected tests. The results appear as underlines and icons without any pause in typing.
Phone apps follow the same rule in a stricter form. The main thread owns the screen. A network call or a large file read runs on a background thread, and the result is posted back to the main thread to update the view. Android enforces this with a NetworkOnMainThreadException.
Number crunching: encoders and pi calculators
Video encoders split work by frame or by slice and give each piece to a thread. On an 8-core machine, x264 runs about 6 to 8 times faster than on one core because the pieces are nearly independent.
The record computations of pi use y-cruncher, a multithreaded program that spreads enormous multiplications across every core and disk in the machine. A computation to 100 trillion digits ran for months on a single large server. Without multithreading it would have taken years.
How to Prepare
Interviewers who ask for examples usually follow with "what could go wrong". Prepare both halves.
- Name the threads, not just the program. "A game has a render thread and a simulation thread" is a stronger answer than "games use multithreading".
- Name the shared data. In the browser example, the main thread and the compositor share layer trees. Explain how the two avoid reading half-written data.
- Be ready to code one example. A producer and consumer sharing a bounded queue is the most common request.
Grokking Multithreading and Concurrency for Coding Interviews builds these examples into coded exercises with locks, semaphores and thread pools.
Grokking the Coding Interview covers the algorithm patterns that appear in the same loop.
Grokking the System Design Interview shows how the web server example scales to many machines.

GET YOUR FREE
Coding Questions Catalog

$99

$197

$72