/** * @author keming.chen * @date 2021/6/17 17:12 * @desc 解析用定制线程池 **/ public class ParseThreadPools { /** * 默认使用线程数 = cpu核数*2 */ public static final int MAX_THREADS = Runtime.getRuntime().availableProcessors(); public static final int DOCUMENT_CORE_POOL_SIZE = 3; public static final ThreadPoolExecutor DOCUMENT_THREAD_POOL = new ThreadPoolExecutor(DOCUMENT_CORE_POOL_SIZE, DOCUMENT_CORE_POOL_SIZE, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new ParseThreadFactory("document")); public static final ThreadPoolExecutor PARSE_THREAD_POOL = new ThreadPoolExecutor( MAX_THREADS, 3 * MAX_THREADS, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new ParseThreadFactory("ocr-marking")); public static final ScheduledThreadPoolExecutor SCHEDULE_POOL = new ScheduledThreadPoolExecutor(1, new ParseThreadFactory("news-report")); public static final ScheduledThreadPoolExecutor SCHEDULE_ANNOUNCE_POOL = new ScheduledThreadPoolExecutor(1, new ParseThreadFactory("parse-announce")); public static final ScheduledThreadPoolExecutor SCHEDULE_REPEAT_POOL = new ScheduledThreadPoolExecutor(1, new ParseThreadFactory("data-repeat")); public static final ScheduledThreadPoolExecutor SCHEDULE_ELECTION_POOL = new ScheduledThreadPoolExecutor(1, new ParseThreadFactory("election")); }
文档解析的时候,我们会指定3个核心线程,最大线程也是3,阻塞队列是 LinkedBlockingQueue
如果不指定线程工厂时,ThreadPoolExecutor 会使用ThreadPoolExecutor.defaultThreadFactory 创建线程。默认工厂创建的线程:同属于相同的线程组,具有同为 Thread.NORM_PRIORITY 的优先级,以及名为 “pool-XXX-thread-” 的线程名(XXX为创建线程时顺序序号),且创建的线程都是非守护进程。