实现一个异步并发调度器
Willem Zhang Lv6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Scheduler {
constructor(limit) {
this.limit = limit
this.number = 0
this.queue = []
}
addTask(timeout, str) {
this.queue.push([timeout, str])
}
start() {
if (this.number < this.limit&&this.queue.length) {
var [timeout, str] = this.queue.shift()
this.number++
setTimeout(() => {
console.log(str)
this.number--
this.start()
}, timeout * 1000);
this.start()
}
}
}

ref

https://juejin.cn/post/6913493585363599373

  • Post title:实现一个异步并发调度器
  • Post author:Willem Zhang
  • Create time:2022-03-29 23:05:24
  • Post link:https://ataraxia.top/2022/03/29/实现一个异步并发调度器/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
 Comments