完成了四种拒绝策略,支持核心线程数常驻,最大线程数超时清扫。
import java.util.LinkedList;
import java.util.Queue;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentLinkedQueue;
public class ThreadPool {
public final int cleanInterval = 1000;
public static final int AbortPolicy = 0;
public static final int CallerRunsPolicy = 1;
public static final int DiscardPolicy = 2;
public static final int DiscardOldestPolicy = 3;
private final int refusePolicy;
private final int lifeTime;
private final int coreLimit;
private final int maxLimit;
private int waitLimit;
private int nowCnt;
private Timer timer;
private boolean isRunning;
private Queue<Task> waitingList = new LinkedList<>();
private Queue<Thd> freeList = new LinkedList<>();
public ThreadPool(int coreLimit, int maxLimit, int waitLimit, int lifeTime, int refusePolicy ) {
this.lifeTime = lifeTime;
this.coreLimit = coreLimit;
this.maxLimit = maxLimit;
this.waitLimit = waitLimit;
this.refusePolicy = refusePolicy;
this.isRunning = true;
setupCleaner();
}
public synchronized void close() {
this.isRunning = false;
}
private void setupCleaner() {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
clearDeadThread();
}
}, cleanInterval, cleanInterval);
}
private synchronized void addToPool(Thd thd) {
freeList.add(thd);
}
private synchronized Task getWaitingTask() {
return waitingList.poll();
}
private synchronized Thd getFreeThread() {
Thd ret = freeList.poll();
while (ret != null && nowCnt > coreLimit && System.currentTimeMillis() - ret.timeStamp > lifeTime) {
nowCnt --;
ret = freeList.poll();
}
return ret;
}
private synchronized void clearDeadThread() {
if(nowCnt <= coreLimit) {
return;
}
Queue<Thd> tmp = new LinkedList<>();
while (!freeList.isEmpty()) {
Thd thd = getFreeThread();
if (thd != null) {
tmp.add(thd);
}
}
freeList.addAll(tmp);
}
public boolean submit(Task task) throws Exception {
boolean accept = true;
synchronized (this) {
if (isRunning) {
Thd thd = getFreeThread();
if (thd != null) {
thd.excuteTask(task);
} else if (nowCnt < coreLimit) {
new Thd(task).start();
} else if (waitingList.size() < waitLimit) {
waitingList.add(task);
} else if (nowCnt < maxLimit) {
new Thd(task).start();
} else if (refusePolicy == DiscardOldestPolicy) {
waitingList.poll();
waitingList.add(task);
} else {
accept = false;
}
} else {
accept = false;
}
}
if (!accept) {
switch (refusePolicy) {
case AbortPolicy:
throw new Exception("submission failed");
case CallerRunsPolicy:
task.work();
break;
case DiscardPolicy:
break;
}
}
return accept;
}
public interface Task {
void work() throws InterruptedException;
}
private class Thd extends Thread {
private long timeStamp;
private Task task;
public synchronized void excuteTask(Task task) {
this.task = task;
this.notify();
}
public Thd(Task task) {
this.task = task;
nowCnt ++;
}
@Override
public void run() {
while (true) {
while (task != null) {
try {
task.work();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
task = getWaitingTask();
}
synchronized (this) {
try {
timeStamp = System.currentTimeMillis();
addToPool(this);
wait();
} catch (Exception ignored) {
}
}
}
}
}
}