// JS实现一个带并发限制的异步调度器Scheduler,
// 保证同时运行的任务最多有两个。
// 完善代码中Scheduler类,
// 使得以下程序能正确输出
class Scheduler {
constructor() {
this.count = 2
this.queue = []
this.run = []
}
add(task) {
// ...
}
}
const timeout = (time) => new Promise(resolve => {
setTimeout(resolve, time)
})
const scheduler = new Scheduler()
const addTask = (time, order) => {
scheduler.add(() => timeout(time)).then(() => console.log(order))
}
addTask(1000, '1')
addTask(500, '2')
addTask(300, '3')
addTask(400, '4')
// output: 2 3 1 4
// 一开始,1、2两个任务进入队列
// 500ms时,2完成,输出2,任务3进队
// 800ms时,3完成,输出3,任务4进队
// 1000ms时,1完成,输出1
// 1200ms时,4完成,输出4
代码
class Scheduler {
constructor(count) {
this.count = count;
this.queue = [];
this.run = [];
}
add(task) {
return new Promise((resolve, reject) => {
this.queue.push({ task, resolve, reject });
if (this.run.length < this.count) {
this._runTask();
}
});
}
_runTask() {
const { task, resolve, reject } = this.queue.shift();
this.run.push(task);
task().then((result) => {
resolve(result);
}, (error) => {
reject(error);
})
.finally(() => {
this.run.splice(this.run.indexOf(task), 1);
if (this.queue.length > 0) {
this._runTask();
}
});
}
}
标签:count,异步,task,run,addTask,JS,resolve,Scheduler
From: https://www.cnblogs.com/pangqianjin/p/17226780.html