Thread pool custom exception handling method

// 线程工厂,用于为线程池中的每条线程命名
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("judge-pool-%d")
                .setUncaughtExceptionHandler((thread, throwable)-> logger.error("ThreadPool {} got exception", thread,throwable))
                .build();

// 创建线程池,使用有界阻塞队列防止内存溢出
ExecutorService statsThreadPool = new ThreadPoolExecutor(5, 10,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(100), namedThreadFactory);

// 遍历所有中心,为每一个centerId提交一条任务到线程池
statsThreadPool.submit(new StatsJob(centerId));



 

Guess you like

Origin blog.csdn.net/xx897115293/article/details/108533263