Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← Fundamentals of Object Oriented Programming Level Three · Lesson 7 of 9

Module Six

📖 Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
7
Module 6: Concurrency & Thread Safety for Young Coders
⭐ Module 6 · Level Three

Concurrency &
Thread Safety

⚡ Make your objects work together without crashing

🎯 What you'll learn in this module

  • ✔ The difference between threads and processes
  • ✔ How race conditions, deadlocks, and livelocks sneak in
  • ✔ How to protect shared data with locks, mutexes, semaphores, and monitors
  • ✔ Why immutable objects are the safest objects of all
  • ✔ How async/await and futures fit into object-oriented design
  • ✔ How actors and message‑passing keep concurrency clean
🧵
Threads vs. Processes: Two Kinds of Workers
Basics

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.

🧵 Thread A
⇄
🧵 Thread B
⇄
🗄️ Shared Memory
🏠 Processes = separate houses. Threads = flatmates sharing a kitchen — more efficient, but someone has to clean up.
🏁
Race Conditions & Deadlocks
Danger Zone

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.

🍽️ Race: two people grab the last slice of pizza at the same moment. Deadlock: two people in a doorway, both waiting for the other.
🔒
Locks, Mutexes & Semaphores
Tools

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.

🚻 Lock = a single bathroom with one key. Semaphore = a row of stalls with a counter: whoever has a stall token can enter.
🧊
Immutable Objects: The Safest Design
Best Practice

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.

📜 A printed certificate: once written and signed, nobody can edit it. Everyone can read it safely, at the same time.
🌀
Async/Await & Futures
Modern

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.

🍔 Ordering food at a restaurant: they give you a buzzer (the future). You sit and chat until the buzzer goes off (async/await).
🎭
Actors & Message Passing
Advanced

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.

📬 Actors are like pen pals: you send them a letter, they reply when ready. You never walk into their house and rearrange their furniture.
⚠️ Concurrency rule of thumb: Start with immutability. If data must change, put it behind a lock or hand it to a single actor. Reaching for synchronized or a lock is a sign to pause and think — is there a simpler design?

🛠️ Hands-on activities (no boring stuff!)

1

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.

2

Thread vs process: Run the same task (e.g., summing 10 million numbers) with threads and with separate processes. Time both. Explain the difference.

3

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.

4

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.

5

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.

6

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.

🏆 Get Certified

🔒

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

🎓 Sign Up to Get Certified
🛠️ Practice Tools
Hands-on simulators & labs - subscription required.
→
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
→