request-job.js
699 Bytes
const lockup = require('node-lockup');
class ThreadJob {
constructor({
jobs, thread, splitTime
}) {
this.jobs = jobs;
this.thread = thread;
this.splitTime = splitTime;
}
start() {
const threadJobs = Array.from({length: this.thread}).map((v, tid) => {
const jobs = [];
this.jobs.forEach((job, inx) => {
if (inx % this.thread === tid) {
jobs.push(job);
}
})
return jobs;
});
threadJobs.forEach((jobs) => {
const locktask = lockup(this._task, this.splitTime || 0);
jobs.forEach(job => {
locktask(job);
})
});
}
_task(job) {
return job();
}
}
module.exports = ThreadJob;