Multithreading allows multiple parts of a program to run concurrently. Each thread has its own stack (local variables, method calls) but shares the same heap (objects). This enables better CPU utilization and responsive applications. However, shared access to the heap requires careful synchronization to prevent data corruption.
A fast-food kitchen. One person flips burgers (Thread 1), another toasts buns (Thread 2), and another pours drinks (Thread 3). They all share the same kitchen space (Heap) but have their own work areas (Stacks).
What is a 'Race Condition'? Provide an example and explain how you would prevent it.