1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| 1. newSingleThreadExecutor -> return new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); 只使用一个线程、使用无界队列 LinkedBlockingQueue, 线程创建后不会超时终止、该线程顺序执行所有任务 适用于需要确保所有任务呗顺序执行的场合
1. newFixedThreadPool -> ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>()) 保持固定线程数、新的任务进入、会放进任务队列 2. newCachedThreadPool -> ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()) 任务到达、有空闲线程就复用、无空闲线程就创建 任务执行完、线程最大空闲时间60s、超过60s会被销毁 3. new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 3, 200, 1,TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(100000), new DefaultThreadFactory())
|