Bird
0
0

Why is it important to check isMainThread before creating a new Worker inside a Node.js script?

hard📝 Conceptual Q10 of 15
Node.js - Worker Threads
Why is it important to check isMainThread before creating a new Worker inside a Node.js script?
ATo ensure workers run only in the main thread
BTo prevent infinite spawning of workers causing a crash
CTo allow workers to share the same event loop
DTo enable workers to access main thread variables directly
Step-by-Step Solution
Solution:
  1. Step 1: Understand worker creation inside scripts

    If a worker script creates a new Worker without checking isMainThread, it will spawn workers endlessly.
  2. Step 2: Consequence of missing check

    This infinite spawning leads to resource exhaustion and crashes the program.
  3. Final Answer:

    To prevent infinite spawning of workers causing a crash -> Option B
  4. Quick Check:

    Check isMainThread to avoid infinite worker creation [OK]
Quick Trick: Check isMainThread to stop infinite worker loops [OK]
Common Mistakes:
  • Thinking workers run in main thread
  • Assuming workers share event loop
  • Believing workers access main thread variables directly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes