0
0
Rubyprogramming~5 mins

Thread creation and execution in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a thread in Ruby?
A thread is a lightweight unit of execution within a Ruby program that allows multiple tasks to run at the same time.
Click to reveal answer
beginner
How do you create a new thread in Ruby?
Use Thread.new { ... } to create and start a new thread that runs the code inside the block.
Click to reveal answer
beginner
What does thread.join do in Ruby?
It makes the main program wait until the thread finishes its work before continuing.
Click to reveal answer
intermediate
Can Ruby threads run truly in parallel?
MRI Ruby threads are native threads and run concurrently but not truly in parallel because of the Global Interpreter Lock (GIL).
Click to reveal answer
intermediate
What happens if you don't join a thread in Ruby?
The main program may finish before the thread completes, causing the thread to be killed prematurely.
Click to reveal answer
How do you start a new thread in Ruby?
Astart_thread()
BThread.start()
CThread.new { code }
Dnew Thread()
What method waits for a thread to finish before continuing?
Await()
Bjoin()
Csleep()
Dstop()
Which Ruby implementation has a Global Interpreter Lock (GIL) affecting threads?
AMRI Ruby
BRubinius
CJRuby
DTruffleRuby
What happens if the main program ends before a thread finishes?
AThread is killed
BThread is paused
CThread continues running in background
DThread restarts
Which of these is a correct way to create and run a thread that prints 'Hello'?
AThread.run { print 'Hello' }
BThread.start { puts 'Hello' }
CThread.create { echo 'Hello' }
DThread.new { puts 'Hello' }
Explain how to create and execute a thread in Ruby and why you might want to use join.
Think about starting a task in the background and waiting for it to finish.
You got /4 concepts.
    Describe the effect of the Global Interpreter Lock (GIL) on Ruby threads and how it impacts parallel execution.
    Consider how threads share the CPU in MRI Ruby.
    You got /4 concepts.