Recall & Review
beginner
What is the purpose of worker threads in Node.js?
Worker threads allow Node.js to run JavaScript code in parallel on multiple threads, helping to perform CPU-intensive tasks without blocking the main event loop.
Click to reveal answer
beginner
How do you create a new worker thread in Node.js?
You create a new worker thread by importing the Worker class from the 'worker_threads' module and then instantiating it with a path to a JavaScript file to run in the worker.Click to reveal answer
intermediate
What method is used to send messages between the main thread and a worker thread?
The 'postMessage' method is used to send messages, and the 'message' event is listened to receive messages between the main thread and worker threads.
Click to reveal answer
beginner
Why should you avoid blocking the main thread in Node.js?
Blocking the main thread stops Node.js from handling other tasks like I/O or user requests, making the app unresponsive. Worker threads help by running heavy tasks separately.
Click to reveal answer
intermediate
What is the difference between worker threads and child processes in Node.js?
Worker threads share memory and are lighter weight, making communication faster. Child processes run in separate memory spaces and are heavier but can run different programs.
Click to reveal answer
Which module do you import to use worker threads in Node.js?
✗ Incorrect
The 'worker_threads' module provides the Worker class to create worker threads.
How do you send data from the main thread to a worker thread?
✗ Incorrect
The 'postMessage' method sends messages between the main thread and worker threads.
What happens if you run a CPU-heavy task on the main thread in Node.js?
✗ Incorrect
CPU-heavy tasks block the main thread, making the app unresponsive.
Which event do you listen to in a worker thread to receive messages?
✗ Incorrect
The 'message' event is used to receive messages in worker threads.
What is a key advantage of worker threads over child processes?
✗ Incorrect
Worker threads share memory, making communication faster and lighter weight than child processes.
Explain how to create and communicate with a worker thread in Node.js.
Think about how the main thread and worker thread talk to each other.
You got /4 concepts.
Describe why worker threads are useful in Node.js applications.
Consider what happens when heavy tasks run on the main thread.
You got /4 concepts.