常春岛资源网 Design By www.syssdc.com
一、前言
"htmlcode">
const task = timeout => new Promise((resolve) => setTimeout(() => { resolve(timeout); }, timeout)) const taskList = [1000, 3000, 200, 1300, 800, 2000]; async function startNoConcurrentControl() { console.time(NO_CONCURRENT_CONTROL_LOG); await Promise.all(taskList.map(item => task(item))); console.timeEnd(NO_CONCURRENT_CONTROL_LOG); } startNoConcurrentControl();
"htmlcode">
class Queue { constructor() { this._queue = []; } push(value) { return this._queue.push(value); } shift() { return this._queue.shift(); } isEmpty() { return this._queue.length === 0; } }
"htmlcode">
class DelayedTask { constructor(resolve, fn, args) { this.resolve = resolve; this.fn = fn; this.args = args; } }
"htmlcode">
class TaskPool { constructor(size) { this.size = size; this.queue = new Queue(); } addTask(fn, args) { return new Promise((resolve) => { this.queue.push(new DelayedTask(resolve, fn, args)); if (this.size) { this.size--; const { resolve: taskResole, fn, args } = this.queue.shift(); taskResole(this.runTask(fn, args)); } }) } pullTask() { if (this.queue.isEmpty()) { return; } if (this.size === 0) { return; } this.size++; const { resolve, fn, args } = this.queue.shift(); resolve(this.runTask(fn, args)); } runTask(fn, args) { const result = Promise.resolve(fn(...args)); result.then(() => { this.size--; this.pullTask(); }).catch(() => { this.size--; this.pullTask(); }) return result; } }
TaskPool 包含三个关键方法:
- addTask: 将新的任务放入队列当中,并触发任务池状态检测,如果当前任务池非满载状态,则从队列中取出任务放入任务池中执行。
- runTask: 执行当前任务,任务执行完成之后,更新任务池状态,此时触发主动拉取新任务的机制。
- pullTask: 如果当前队列不为空,且任务池不满载,则主动取出队列中的任务执行。
"htmlcode">
const cc = new ConcurrentControl(2); async function startConcurrentControl() { console.time(CONCURRENT_CONTROL_LOG); await Promise.all(taskList.map(item => cc.addTask(task, [item]))) console.timeEnd(CONCURRENT_CONTROL_LOG); } startConcurrentControl();
"htmlcode">
await Promise.all(taskList.map(item => cc.addTask(task, [item])))
"htmlcode">
addTask(fn) { return (...args) => { return new Promise((resolve) => { this.queue.push(new DelayedTask(resolve, fn, args)); if (this.size) { this.size--; const { resolve: taskResole, fn: taskFn, args: taskArgs } = this.queue.shift(); taskResole(this.runTask(taskFn, taskArgs)); } }) } }
改造之后的代码显得简洁了很多:
await Promise.all(taskList.map(cc.addTask(task)))
五、优化出队操作
"htmlcode">
const Benchmark = require('benchmark'); const suite = new Benchmark.Suite; suite.add('shift', function() { let count = 10; const arr = generateArray(count); while (count--) { arr.shift(); } }) .add('reverse + pop', function() { let count = 10; const arr = generateArray(count); arr.reverse(); while (count--) { arr.pop(); } }) .on('cycle', function(event) { console.log(String(event.target)); }) .on('complete', function() { console.log('Fastest is ' + this.filter('fastest').map('name')); console.log('\n') }) .run({ async: true })
通过 benchmark.js 跑出的基准测试数据,可以很容易地看出哪种方式的效率更高:
"htmlcode">
class HighPerformanceQueue { constructor() { this.q1 = []; // 用于 push 数据 this.q2 = []; // 用于 shift 数据 } push(value) { return this.q1.push(value); } shift() { let q2 = this.q2; if (q2.length === 0) { const q1 = this.q1; if (q1.length === 0) { return; } q2 = this.q2 = q1.reverse(); } return q2.pop(); } isEmpty() { if (this.q1.length === 0 && this.q2.length === 0) { return true; } return false; } }
最后通过基准测试来验证优化的效果:
标签:
js,并发控制,js实现并发控制
常春岛资源网 Design By www.syssdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
常春岛资源网 Design By www.syssdc.com
暂无评论...
更新日志
2024年06月03日
2024年06月03日
- 罗斯特洛波维奇、布里顿《舒伯特-阿佩乔尼奏鸣曲》1995[WAV+CUE]
- 群星《超高品质流行女声大碟 静听女人心 2CD》[WAV+CUE][1.3GB]
- BEYOND《无悔这一生 2023》 [WAV+CUE][690MB]
- 群星《30首最发烧经典老歌 情牵男人心 2CD》[WAV+CUE][1.4GB]
- 群星《不老的情歌-至尊版2CD》WAV分轨
- 岁月留声机群星《七十年代人金曲回顾》(黑胶)2CD[WAV分轨]
- 群星-世纪难忘金曲(迪安唱片)5CD[WAV]
- HenrietteFaure-DebussyEstampesPrludes,LivreI(MonoVersion)[FLAC]
- 吕方1992-弯弯十八首[香港][WAV+CUE]
- 经典歌曲珍藏专辑《民歌30年·真情依旧》3CD[WAV+CUE]
- 群星《时尚流行发烧极品 非同凡响 第一辑 2CD》[WAV/分轨][1.3GB]
- 陈曦《多年以后》HQⅡ头版限量编号[WAV+CUE][520MB]
- 孙露《用情 HQⅡ 2023》[WAV+CUE][380MB]
- 电影原声《速度与激情9电影原声大碟》2021[WAV+CUE]
- 【瑞鸣音乐】群星《伶歌》[XRCD2][WAV+CUE]