TaskPool 类

Posted 沿着路走到底

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TaskPool 类相关的知识,希望对你有一定的参考价值。

1

class TaskPool 
  tasks = []
  timer = 0
  delayRun(delayTime, callback) 
     console.log('register', delayTime, callback.name)
     this.register(delayTime, callback)
     return this
  

  register(delayTime, callback) 
    this.tasks.push(
      delayTime, 
      callback
    )
    if (this.timer) 
      clearTimeout(this.timer)
    
    this.timer = setTimeout(() => 
      this.run()
    , 200)
  

  run() 
    const fn = this.tasks.shift()
    setTimeout(() => 
      fn.callback()
      if (this.tasks.length) 
        this.run()
      
    , fn.delayTime)
  


const instance = new TaskPool();

instance
  .delayRun(3000, function task1() 
    console.log("run log 1");
  )
  .delayRun(2000, function task2() 
    console.log("run log 2");
  )
  .delayRun(1000, function task3() 
    console.log("run log 3");
  );

setTimeout(() => 
  instance.delayRun(10, function task4() 
    console.log("run log 4");
  );
, 4000);

//打印 register 3000 task1
//打印 register 2000 task2
//打印 register 1000 task3
//过 3 秒打印 run log 1
//打印 register 10 task4
//间隔2秒打印 run log 2
//又间隔1秒打印 run log 3
//又间隔10毫秒打印 run log 4

以上是关于TaskPool 类的主要内容,如果未能解决你的问题,请参考以下文章

GameFramework:Resource加载,资源加载,依赖加载,任务池,对象池,引用计数

d并行两个循环

ForkjoinPool -1

Golang语言社区投稿golang高并发基于协程,通道的任务池

JavaSE基础六----<常用类>Math类Random类|System类,Date类Calendar类SimpleDateFormat类,BigInteger类BigDecimal类

30根据官方教程详解嵌套类内部类静态嵌套类局部类匿名类 ...