packagecom.equator.eagle.threadpool;importlombok.extern.slf4j.Slf4j;importjava.util.LinkedList;importjava.util.List;importjava.util.concurrent.ThreadLocalRandom;importjava.util.concurrent.atomic.AtomicInteger;/*** @Author: Equator
* @Date: 2019/12/4 20:39
**/@Slf4jpublic class DefaultThreadPool implements EagleThreadPool{//线程池工作者数目
private static final int maxWorkerNumber = 16;private static final int defaultWorkerNumber = 4;private static final int minWorkerNumber = 1;//工作者编号(线程名)
private AtomicInteger workerId = newAtomicInteger();//工作者队列
private final List workerList = new LinkedList<>();//工作任务队列
private final List jobList = new LinkedList<>();//是否接受新的任务
private volatile boolean isWorking = true;//工作者内部类
class Worker implementsRunnable {private volatile boolean isRunning = true;private volatile boolean isHandling = false;
@Overridepublic voidrun() {while(isRunning) {
Job job= null;synchronized(jobList) {while (isRunning &&jobList.isEmpty()) {try{//超时等待
jobList.wait(1000);
}catch(InterruptedException e) {
Thread.currentThread().interrupt();return;
}
}//取一个任务,这里需要判断一下任务队列是否为空,否则会发生NPE(空指针异常)
if (jobList.size() > 0) {
job= jobList.remove(0);
}
}if (job != null) {try{
isHandling= true;
job.run();
}catch(Exception e) {
}finally{
isHandling= false;
}
}
}
}public voidclose() {this.isRunning = false;
}
}publicDefaultThreadPool() {
initWorkers(defaultWorkerNumber);
}public DefaultThreadPool(intinitialWorkerNumber) {
initWorkers(initialWorkerNumber);
}public int initWorkers(intnum) {int freeCapacity = maxWorkerNumber -workerList.size();if (num >=freeCapacity) {
num=freeCapacity;
}if (num
num=minWorkerNumber;
}for (int i = 0; i < num; i++) {
Worker worker= newWorker();
workerList.add(worker);
Thread thread= new Thread(worker, "Worker-" +workerId.incrementAndGet());
thread.start();
}returnnum;
}
@Overridepublic voidexecute(Job job) {if (isWorking && job != null) {synchronized(jobList) {
jobList.add(job);
jobList.notify();
}
}else{
log.debug("thread pool is waiting to close or job is null");
}
}
@Overridepublic intgetJobSize() {returnjobList.size();
}
@Overridepublic int addWorkers(intnum) {synchronized(jobList) {returninitWorkers(num);
}
}
@Overridepublic int removeWorkers(intnum) {int count = 0;synchronized(jobList) {for (int i = 0; i < num; i++) {
Worker worker=workerList.get(i);if (!worker.isHandling) {
worker.close();
count++;
}
}
}returncount;
}
@Overridepublic voidshutdown() {
isWorking= false;while (!jobList.isEmpty()) {
log.debug("sorry, jobList is not null, jobList size :{}, waiting to close", jobList.size());
}for(Worker worker : workerList) {
worker.close();
}
}public static voidmain(String[] args) {
DefaultThreadPool defaultThreadPool= newDefaultThreadPool();int count = 1000;while (count > 0) {int finalCount =count;
defaultThreadPool.execute(newRunnable() {
@Overridepublic voidrun() {int flag = 10;while (flag > 0) {
log.debug("job{} say {}", finalCount, flag);
flag--;
}
log.debug("job{} done", finalCount);
}
});if (count == 500) {
defaultThreadPool.shutdown();
}
count--;
}
}
}