多线程处理

@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);
    }
}
上一篇:每日三道面试题,通往*的道路11——线程池


下一篇:高并发面试:线程池的七大参数?手写一个线程池?