Threads and Concurrency

Lesson 19 of 20

Threads run code side by side — spawn them with Thread.new, wait with join, and see why their console output can interleave.

The big picture

Two lines of execution, one program

A thread is a second line of execution inside the same process — it shares your objects and prints to the same console.

Thread.new Spawns

Thread.new { ... } starts the block immediately on its own line of execution.

join Waits

t.join blocks the caller until that thread has completely finished.

Everything Is Shared

Threads see the same objects and globals — shared state needs care.

Timing Is Real

Without sleeps or joins, which thread prints first can change from run to run.