JS相关知识点
#25
Replies: 1 comment
-
Promise 并发通过接口,动态添加任务 interface ITask {
(): Promise<void>
successCallBack?: () => void
}
/**
* 并发限制
*/
export class ThreadPool {
// 任务队列
taskList!: ITask[]
// 并发限制
limit!: number
// 正在执行中的任务数量
pendingCounter!: number
constructor(option: { limit: number }) {
this.taskList = []
this.limit = option.limit
this.pendingCounter = 0
}
// 执行任务
__runNextTask() {
if (this.pendingCounter >= this.limit) return
const task = this.taskList.shift()
if (!task) return
this.pendingCounter = this.pendingCounter + 1
task().finally(() => {
this.pendingCounter = this.pendingCounter - 1
// 调用执行结束函数
const successCallBack = task.successCallBack
if (typeof successCallBack === 'function') successCallBack()
// 执行下一个任务
this.__runNextTask()
})
}
// 动态添加任务
addTask(taskList: ITask, successCallBack?: () => void) {
taskList.successCallBack = successCallBack
this.taskList.push(taskList)
this.__runNextTask()
}
}
// 调用示例
const taskRunner = new ThreadPool({ limit: 1 });
taskRunner.addTask(() => sleep(11));
taskRunner.addTask(() => sleep(22));
taskRunner.addTask(
() => sleep(33),
() => console.log('33 succsss')
);
taskRunner.addTask(() => sleep(44)); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Beta Was this translation helpful? Give feedback.
All reactions