Last active
June 29, 2018 08:11
-
-
Save deleteman/aa19d01fee124239ebf8fd2f5f6134b6 to your computer and use it in GitHub Desktop.
worker-threads-sample1
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, workerData } = require('worker_threads'); | |
let currentVal = 0; | |
let intervals = [100,1000, 500] | |
function counter(id, i){ | |
console.log("[", id, "]", i) | |
return i; | |
} | |
if(isMainThread) { | |
console.log("this is the main thread") | |
for(let i = 0; i < 2; i++) { | |
let w = new Worker(__filename, {workerData: i}); | |
} | |
setInterval((a) => currentVal = counter(a,currentVal + 1), intervals[2], "MainThread"); | |
} else { | |
console.log("this isn't") | |
setInterval((a) => currentVal = counter(a,currentVal + 1), intervals[workerData], workerData); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment