js并发限制

  • JS实现一个带并发限制的异步调度器Scheduler,保证同时运行的任务最多有两个。完善下面代码的Scheduler类,使以下程序能够正常输出:
class Scheduler {
  add(promiseCreator) { ... }
  // ...
}
   
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
  • 题解
class Scheduler {
  executeQueue = [];
  waitQueue = [];
  exeCount = 0;
  add(promiseCreator) {
    if (this.executeQueue.length >= 2) {
      //超过两个时,等待
      this.waitQueue.push(promiseCreator);
      return;
    }

    // 不足2个时入队
    this.executeQueue.push(promiseCreator);
    this.execute();
  }
  execute() {
    if (this.exeCount >= 2) {
      return;
    }
    this.exeCount++;
    let p = this.executeQueue.shift();
    p()
      .then(
        (res) => {
          this.exeCount--;
          if (this.waitQueue.length > 0) {
            this.executeQueue.push(this.waitQueue.shift());
          }
          if (this.executeQueue.length > 0) {
            this.execute();
          }
        },
        (rej) => {
          //把异常抛出来让外部catch到
          throw new Error(rej);
        }
      )
      .catch((err) => {
        console.log(err);
        this.exeCount--;
        if (this.waitQueue.length > 0) {
          this.executeQueue.push(this.waitQueue.shift());
        }
        if (this.executeQueue.length > 0) {
          this.execute();
        }
      });
  }
}

const timeout = (time) =>
  new Promise((resolve, rejects) => {
    if (time === 300) {
      rejects(new Error("xx"));
    } else {
      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");

// scheduler.taskStart();
// normal output: 2 3 1 4
// error output: 2 Error 4 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值