0
0
Rubyprogramming~10 mins

Thread creation and execution in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Thread creation and execution
Start main program
Create new thread
Thread starts running block
Thread executes code
Thread finishes
Main program continues or waits
Program ends
The main program creates a thread that runs code concurrently. The thread executes its block and finishes, while the main program can continue or wait.
Execution Sample
Ruby
thread = Thread.new do
  puts "Hello from thread"
end
thread.join
puts "Main program ends"
This code creates a thread that prints a message, waits for it to finish, then prints a final message.
Execution Table
StepActionThread StateOutputMain Program State
1Main program startsNo thread yetRunning
2Create new thread with blockNew thread createdRunning
3Thread starts running blockRunningHello from threadRunning
4Thread finishes blockFinishedHello from threadWaiting at join
5Main program resumes after joinFinishedHello from threadRunning
6Main program prints final messageFinishedHello from thread Main program endsRunning
7Program endsFinishedHello from thread Main program endsExited
💡 Thread finishes execution and main program ends after printing all messages.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
threadnilThread object createdThread finishedThread finished
Key Moments - 2 Insights
Why does the main program wait at thread.join?
Because thread.join pauses the main program until the thread finishes, as shown in execution_table step 4.
What happens if we don't call thread.join?
The main program may finish before the thread runs or completes, so the thread's output might not appear, unlike step 4 where join waits.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the thread state at step 3?
ANew thread created
BFinished
CRunning
DNo thread yet
💡 Hint
Check the 'Thread State' column at step 3 in the execution_table.
At which step does the main program wait for the thread to finish?
AStep 4
BStep 3
CStep 2
DStep 6
💡 Hint
Look at the 'Main Program State' column to find when it is 'Waiting at join'.
If we remove thread.join, what will likely happen to the output?
ABoth messages print in order
BOnly 'Main program ends' prints
COnly 'Hello from thread' prints
DNo output prints
💡 Hint
Without join, main program may finish before thread runs, so only main message prints.
Concept Snapshot
Thread creation in Ruby:
thread = Thread.new { code }
Thread runs code concurrently.
Use thread.join to wait for thread to finish.
Without join, main may finish first.
Threads allow parallel tasks.
Full Transcript
This example shows how Ruby creates and runs a thread. The main program starts and creates a new thread with Thread.new. The thread runs the code block, printing 'Hello from thread'. The main program calls thread.join to wait until the thread finishes. After the thread ends, the main program prints 'Main program ends' and then exits. Variables like 'thread' hold the thread object, which changes state from created to finished. Key points include understanding that join pauses the main program until the thread completes, and without join, the main program might finish before the thread runs. The execution table tracks each step, thread state, output, and main program state to visualize the flow.