Bird
0
0

What is the immediate effect of executing Thread.new { puts 'Running' } in Ruby?

easy📝 Conceptual Q1 of 15
Ruby - Concurrent Programming
What is the immediate effect of executing Thread.new { puts 'Running' } in Ruby?
AIt creates and starts a new thread that runs the given block concurrently.
BIt only defines a thread but does not start it until <code>start</code> is called.
CIt pauses the main thread until the new thread finishes execution.
DIt creates a thread but runs the block synchronously in the main thread.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Thread.new

    This method creates a new thread and immediately starts executing the block passed to it concurrently with the main thread.
  2. Step 2: Analyze options

    It creates and starts a new thread that runs the given block concurrently. correctly states that the thread is created and started immediately. It only defines a thread but does not start it until start is called. is incorrect because Ruby threads start immediately upon creation. It pauses the main thread until the new thread finishes execution. is wrong because the main thread continues unless join is called. It creates a thread but runs the block synchronously in the main thread. is false as the block runs in the new thread, not synchronously.
  3. Final Answer:

    It creates and starts a new thread that runs the given block concurrently. -> Option A
  4. Quick Check:

    Thread.new starts thread immediately [OK]
Quick Trick: Thread.new starts the thread immediately [OK]
Common Mistakes:
  • Thinking Thread.new only defines but does not start the thread
  • Assuming the main thread waits automatically for new threads
  • Believing the block runs synchronously in the main thread

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes