Process vs Thread
Two ways your program runs â and why threads are 'lightweight'.
What is a Process?
A process is an independent program in execution, with its own memory space (code, data, heap, stack). Processes don't share memory with each other by default, making them isolated but heavier to create.
What is a Thread?
A thread is a unit of execution within a process. Multiple threads in the same process share the same memory space, making them faster to create and switch between â but requiring careful synchronization to avoid conflicts.
Key Difference
Processes are isolated and heavyweight; threads are shared and lightweight. Multi-threading is used for tasks like handling multiple users on a server simultaneously without the overhead of separate processes.
đ Real-World Use
When you open multiple Chrome tabs, each is often a separate process (so one crashing tab doesn't crash your whole browser). Inside a single app like a game, multiple threads handle graphics rendering, sound, and input simultaneously.
đĄ Pro Tip
A classic interview follow-up: 'why do threads need synchronization but processes usually don't?' â answer: because threads share memory, so two threads writing to the same variable at once can corrupt data (a 'race condition'), which processes avoid by having separate memory.
đ§Ē Quick Self-Test
Check what you just learned â no pressure, just practice.
1. Do threads within the same process share memory?
2. Why are threads called 'lightweight'?