@Component
public class OkThreadPool {
private static final Integer CORE_POOL_SIZE = 20;
private static final Integer MAX_POOL_SIZE = 40;
private static final Integer KEEP_ALIVE_TIME = 60;
private static final Integer POOL_CAPACITY = 1000;
private static ThreadPoolExecutor pool;
@PostConstruct
static public void init() {
pool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(POOL_CAPACITY),
(r) -> {
Thread t = new Thread(r);
String threadName = "Ok Thread";
t.setDaemon(false);
t.setName(threadName);
return t;
},
new ThreadPoolExecutor.CallerRunsPolicy());
}
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) {
return CompletableFuture.supplyAsync(supplier, pool);
}
public static CompletableFuture<Void> runAsync(Runnable runnable) {
return CompletableFuture.runAsync(runnable, pool);
}
}