io.github.linmoure.thread.handler.RejectedExecutionHandler Maven / Gradle / Ivy
package io.github.linmoure.thread.handler;
import io.github.linmoure.thread.config.ThreadPoolConfig;
import io.github.linmoure.thread.config.ThreadScheduleConfig;
import io.github.linmoure.thread.schedule.ThreadSchedule;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
public class RejectedExecutionHandler implements java.util.concurrent.RejectedExecutionHandler {
private final ThreadPoolConfig config;
public RejectedExecutionHandler(ThreadPoolConfig config) {
this.config = config;
}
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
switch (config.getRejectedPolicy()) {
case ThreadPoolConfig.REJECTED_POLICY_0: {
throw new RejectedExecutionException("one task rejected, pool=" + this.config.getName());
}
case ThreadPoolConfig.REJECTED_POLICY_1: {
if (executor != null && !executor.isShutdown()) {
r.run();
}
}
break;
case ThreadPoolConfig.REJECTED_POLICY_2: {
if (config.getQueueCapacity() > 0) {
ThreadSchedule.BUFFER_QUEUE.offer(new ThreadScheduleConfig() {{
setRunnable(r);
setConfig(config);
}});
}
}
break;
default:
throw new RejectedExecutionException("one task rejected, pool=" + this.config.getName());
}
}
}