HiExecutor

2020-10-20  本文已影响0人  dashu52

全局通用的线程池组件-HiExecutor

HiExecutor.execute(10, new Runnable {
    ...do what you want.      
});
 
void pause()//暂停线程池
void reume()//恢复线程池
void execute(int priority,Runnable runnable)//仍给线程池结果不管
void execute(int priority,Callable<T> runnable)//可以拿到任务执行的结果

Coding实现

int corePoolSize=cpuCount+1;
int maximumPoolSize= cpuCount*2+1;
BlockingQueue<Runnable> workQueue= new PriorityBlockingQueue()

new ThreadPoolExecutor(
            corePoolSize,
            maxPoolSize,
            keepAliveTime
            TimeUnit.SECONDS
            new PriorityBlockingQueue,
            threadFactory)
class PriorityRunnable(int priority,Runnable runnable) implements Runnable, Comparable<PriorityRunnable>{
  @override
  public  void run() {
       //让真正的runnable任务去执行
       runnable.run()
  }
  
  @override
  public int compareTo(PriorityRunnable other) {
    //实现Comparable接口,复写该方法,用以比较各任务的优先级排序
      return if (this.priority < other.priority) 1 else if (this.priority > other.priority) -1 else 0
    }
}
}
class ThreadPoolExecutor{
  ReentrantLock lock = new ReentrantLock();
  Condition pauseCondition = lock.newCondition();
  void beforeExecute(Thread thread, Runnable runnable){
    if(isPaused){
    try{
      //使当前线程阻塞
        lock.lock();
        pauseCondition.await()
    }finally{
      lock.unlock();
    }
  }
 }
}

abstract class Callable<T> implements Runnable {
  @override     
  public void run() {
          mainHandler.post { onPrepare() }

          T t = onBackground()

          mainHandler.post { onCompleted(t) }
        }

  public void onPrepare() {
       //任务开始前的准备,比如转菊花
  }
  //子线程真正的去执行
  abstract T onBackground()
          
  //执行完抛到主线程
   abstract void onCompleted(T t)
 }

上一篇 下一篇

猜你喜欢

热点阅读