实现一个并行任务调度器:
- 实现一个任务调度器
scheduler - 只能同时执行 n 个任务
- 当有 n 个任务正在执行时,后安排的任务要排队
/**
* 并发控制的Promise调度器
* 限制同时运行的任务数量,确保资源利用最大化
* 适用于异步任务队列管理,如网络请求、文件处理等
* @class PromiseScheduler
*/
class PromiseScheduler {
constructor(maxConcurrent = 2) {
this.maxConcurrent = maxConcurrent; // 最大并发数
this.runningCount = 0; // 当前运行中的任务数
this.taskQueue = []; // 等待队列
}
/**
* 添加任务到调度器
* @param {() => Promise<any>} task - 返回Promise的任务函数
* @returns {Promise<any>} 任务执行完成的Promise
*/
add(task) {
return new Promise((resolve, reject) => {
// 将任务包装成可控制的队列元素
this.taskQueue.push({
id: Date.now(),
task,
resolve,
reject
});
this._run(); // 尝试执行任务
});
}
/** 内部方法:执行任务调度 */
_run() {
// 当有空闲且队列有任务时继续执行
if (this.runningCount < this.maxConcurrent && this.taskQueue.length > 0) {
const queueItem = this.taskQueue.shift(); // 取出队列第一个任务
this.runningCount++;
// 执行实际任务
queueItem.task()
.then((result) => {
queueItem.resolve({
id: queueItem.id,
result
}); // 传递任务结果
})
.catch((err) => {
queueItem.reject(err); // 传递错误
})
.finally(() => {
this.runningCount--; // 释放并发计数
this._run(); // 递归调用以执行后续任务
});
}
}