Last active
September 14, 2020 09:24
-
-
Save SK-CSE/675318a87a910c5a66e8ce764dc585fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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