Skip to content

Instantly share code, notes, and snippets.

@pprathameshmore
Created May 7, 2020 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pprathameshmore/980146aa85668299001648a27b90eb15 to your computer and use it in GitHub Desktop.
Save pprathameshmore/980146aa85668299001648a27b90eb15 to your computer and use it in GitHub Desktop.
const { Worker, isMainThread, parentPort } = require('worker_threads');
if (isMainThread) {
console.log('Starting the main thread');
const worker = new Worker(__filename);
worker.on('message', (msg) => {
console.log(`Worker: ${msg}`);
});
console.log('Still in main thread');
} else {
parentPort.postMessage('Getting started');
wasteTime();
parentPort.postMessage('In the middle');
wasteTime();
parentPort.postMessage('All done');
}
function wasteTime() {
const end = Date.now() + 3000;
while (Date.now() < end) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment