实现Scheduler
# 实现Scheduler
JS实现一个带并发限制的异步调度器Scheduler,保证同时运行的任务最多有两个。完善代码中Scheduler类,使得以下程序能正确输出。
class Scheduler {
add() {}
}
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Scheduler {
constructor(max) {
this.max = max;
this.count = 0;
this.queue = [];
}
async add(fn) {
if (this.count >= this.max) {
await new Promise(resolve => this.queue.push(resolve));
}
this.count++;
const res = await fn();
this.count--;
this.queue.length && this.queue.shift()();
return res;
}
}
class Scheduler {
constructor() {
this.max = 2 // 最大并发任务数
this.count = 0 // 当前并发任务数
this.queue = [] // 阻塞的任务队列
}
async add (fn) {
if (this.count >= this.max) {
/**
* 若当前正在执行的任务达到最大容量max
* 阻塞在此处,等待前面的任务执行完毕后将resolve弹出并执行
*/
await new Promise (resolve => this.queue.push(resolve))
}
this.count++
const res = await fn() // 使用 await 执行此函数
this.count--
this.queue.length && this.queue.shift()() // 若队列中有值,则将resolve弹出并执行
return res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Scheduler {
constructor() {
this.list = [];
this.count = 0;
}
add(promiseCreator) {
return new Promise((resolve) => {
this.list.push({ promiseCreator, resolve });
this.run();
});
}
run() {
if (this.list.length && this.count < 2) {
this.count++;
let { promiseCreator, resolve } = this.list.shift();
Promise.resolve(promiseCreator()).then(() => {
resolve();
this.count--;
this.run();
});
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23