Threads let your program do many things at the same time. This helps make your program faster and more responsive.
Thread creation and execution in Ruby
thread = Thread.new do # code to run in the thread end thread.join # wait for the thread to finish
The Thread.new method starts a new thread and runs the code inside the block.
Use thread.join to wait for the thread to finish before continuing.
thread = Thread.new do puts "Hello from thread!" end thread.join
thread1 = Thread.new { puts "First thread" } thread2 = Thread.new { puts "Second thread" } thread1.join thread2.join
thread = Thread.new do 3.times do |i| puts "Count: #{i}" sleep(1) end end thread.join
This program starts a thread that counts to 4 with pauses. Meanwhile, the main program prints a message and waits for the thread to finish before printing the last message.
thread = Thread.new do 5.times do |i| puts "Thread counting: #{i}" sleep(0.5) end end puts "Main program continues" thread.join puts "Thread finished"
Threads share the same memory space, so be careful when changing shared data.
Use sleep to pause a thread without stopping the whole program.
Always join threads if you want to wait for them to finish before ending the program.
Threads let your program do many things at once.
Create threads with Thread.new and run code inside a block.
Use join to wait for threads to finish.
Practice
Thread.new do in Ruby?Solution
Step 1: Understand the purpose of
Thread.newThread.newstarts a new thread that runs the code inside its block concurrently with the main program.Step 2: Compare options with the behavior
Only It creates a new thread to run code concurrently. correctly describes creating a new thread to run code at the same time as other threads.Final Answer:
It creates a new thread to run code concurrently. -> Option AQuick Check:
Thread.new= create thread [OK]
- Confusing thread creation with thread termination
- Thinking Thread.new pauses threads
- Assuming Thread.new modifies methods
Solution
Step 1: Recall correct thread creation syntax
The correct way isThread.new { ... }where the block contains the code to run.Step 2: Check each option
Thread.new { puts 'Hello from thread' } usesThread.newwith a block correctly. Options A, C, and D use invalid or non-existent methods or syntax.Final Answer:
Thread.new { puts 'Hello from thread' } -> Option DQuick Check:
UseThread.new { }for threads [OK]
- Using Thread.start without a block
- Mistyping method names like Thread.create
- Using Thread.run which does not exist
thread = Thread.new { 5.times { |i| puts i; sleep 0.1 } }
thread.join
puts 'Done'Solution
Step 1: Understand thread execution and join
The thread prints numbers 0 to 4 with a short pause. The main thread waits for it to finish because ofthread.join.Step 2: Determine output order
The numbers 0 to 4 print first, each on its own line, then after the thread finishes, 'Done' prints.Final Answer:
0 1 2 3 4 Done (each number on its own line) -> Option AQuick Check:
Thread prints numbers before main prints 'Done' [OK]
- Assuming 'Done' prints before numbers
- Thinking join blocks forever
- Ignoring sleep delays in thread
thread = Thread.new do puts 'Start' sleep 1 puts 'End' end puts 'Waiting' thread.join
Solution
Step 1: Check thread block syntax
The thread block is properly closed withend. Syntax is correct.Step 2: Verify thread execution and join
The main thread prints 'Waiting' then callsthread.jointo wait for the thread to finish. This is correct usage.Final Answer:
There is no error; the code runs correctly. -> Option CQuick Check:
Thread syntax and join usage are correct [OK]
- Thinking sleep is invalid in threads
- Assuming join is missing
- Miscounting block ends
Solution
Step 1: Create threads storing them in an array
threads = [] 3.times do |i| threads << Thread.new { puts i + 1 } end threads.each(&:join) puts 'Finished' correctly creates 3 threads, each printingi + 1(1, 2, 3), and stores them inthreads.Step 2: Wait for all threads to finish before printing 'Finished'
threads = [] 3.times do |i| threads << Thread.new { puts i + 1 } end threads.each(&:join) puts 'Finished' callsthreads.each(&:join)to wait for all threads, then prints 'Finished'. This ensures 'Finished' prints last.Final Answer:
threads = [] 3.times do |i| threads << Thread.new { puts i + 1 } end threads.each(&:join) puts 'Finished' -> Option BQuick Check:
Store threads, join all, then print [OK]
- Not storing threads to join later
- Printing 'Finished' before threads complete
- Using wrong thread index values
Ready to prove it?
Pass the test and this pattern moves unknown
