-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.js
More file actions
31 lines (30 loc) · 1.08 KB
/
Copy pathMain.js
File metadata and controls
31 lines (30 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { Worker, isMainThread, parentPort } from "node:worker_threads";
if (isMainThread) {
const CPU_core = 10;
for (let i = 0; i < CPU_core; i++) {
// Create a new worker thread
const worker = new Worker("./worker.js");
// pass value to the worker thread
worker.postMessage([
`Thread-${i + 1}`,
Math.ceil(Math.random() * 10) * 1000000000,
]);
// Listen for messages from the worker thread
worker.on("message", (result) => {
console.log(`${result} is done.`);
worker.terminate(); // Terminate the worker thread
});
// Handle errors in the worker thread
worker.on("error", (err) => {
console.error(`Worker error: ${err}`);
});
}
console.log("waiting to complete....");
} else {
// This code is executed in the as worker thread mode
parentPort.on("message", (message) => {
console.log(`Received message from Other thread: ${message}`);
// Perform some task (in this case, just echoing the message)
parentPort.postMessage(`Hello from worker! Received: ${message}`);
});
}