A process is a program running on its own — like a separate person working in a separate room, with their own memory. A thread is a lighter worker inside a process, sharing the same memory with its siblings.
Threads start faster and share data easily — but that shared data is exactly where trouble begins. Two threads reading and writing the same variable at once can corrupt it.
A race condition happens when two threads access shared data in the wrong order. Example: two threads both read counter = 5, both add 1, both write back 6 — but the correct answer is 7!
A deadlock is when two threads each hold a lock the other needs. Neither can move. It's like two people in a hallway each saying "no, you go first" — forever. A livelock is similar, but the threads keep moving without making progress.
A lock (or mutex, "mutual exclusion") lets only one thread enter a critical section at a time. Others wait their turn.
A semaphore is a more general counter: it can allow N threads at once. A semaphore with N=1 acts like a mutex; N=5 lets five threads into a room together. Useful for limiting resources — like only three people using a printer at once.
An immutable object can't be changed after creation. That means threads can share it freely — no locks required, no race conditions possible.
Examples: strings in Java and Python, numbers in most languages, and many value types. Need a change? You create a new object rather than mutating the old one. It costs a little memory but saves a lot of headaches.
Async/await lets your code start a task and keep doing other things while it runs — then return when the result is ready. It's like ordering a coffee and sitting down instead of standing at the counter.
A Future (or Promise) is a placeholder for a result that hasn't arrived yet. When the result is ready, the future "resolves" and your code continues. Async/await is built on top of futures — just easier to read.
Actors are objects that talk only by messages — never by touching each other's memory. Each actor has its own private state and a mailbox of incoming messages. It processes messages one at a time, so no race conditions.
This model (popularized by Erlang and Akka) scales well across many cores and even many machines. When two actors need to coordinate, they send messages — not shared memory.
synchronized or a lock is a sign to pause and think — is there a simpler design?
Race condition lab: Create a shared counter and two threads that each add 1,000 to it. Without a lock, run it a few times and see the count come out wrong. Then add a lock and confirm it's always 2,000.
Thread vs process: Run the same task (e.g., summing 10 million numbers) with threads and with separate processes. Time both. Explain the difference.
Deadlock demo: Create two threads that each try to lock two shared resources in opposite order. Watch the program freeze. Then fix the order and see it succeed.
Immutable safe-share: Design a Point class that can't be modified after creation. Share it across threads without any lock and confirm there are no issues.
Async coffee shop: Write an async function that simulates brewing coffee while other tasks run in parallel. Use async/await (Python asyncio or JavaScript) and log the order of events.
Actor mailboxes: Simulate two actors that exchange messages via simple queues. Each actor processes one message at a time. Confirm that neither's private state is touched directly by the other.
By the end of this module, you'll be able to: explain threads vs. processes, spot race conditions and deadlocks, protect shared data with locks and semaphores, design immutable objects, and use async/await and actors safely.