Skip to content

Instantly share code, notes, and snippets.

@SK-CSE
Last active September 14, 2020 09:24
Show Gist options
  • Save SK-CSE/675318a87a910c5a66e8ce764dc585fd to your computer and use it in GitHub Desktop.
Save SK-CSE/675318a87a910c5a66e8ce764dc585fd to your computer and use it in GitHub Desktop.
const { Worker, isMainThread, parentPort } = require('worker_threads');
if(isMainThread) {
console.log('main thread start...');
const worker = new Worker(__filename);
worker.on('message', (msg) => {
console.log(`Worker: ${msg}`);
});
console.log("doing some random work in main thread..!!");
}else{
parentPort.postMessage('hello from worker thread');
cpuIntensiveTask(1000);
parentPort.postMessage('i am working');
cpuIntensiveTask(1000);
parentPort.postMessage('task is done..!!');
}
function cpuIntensiveTask(timeInSecond) {
const end = Date.now() + timeInSecond;
while (Date.now() < end) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment