Executor框架的主要成员:ThreadPoolExecutor、ScheduledThreadPoolExecutor、Future接口、runnable接口、Callable接口和Executors。
通常使用工厂类executors来创建。executors可创建3种类型的ThreadPoolExecutor :SingleThreadExecutor、FixedThreadPool和CachedThreadPool。
1) FixedThreadPool
FixedThreadPool 被称为可重用固定线程数的线程池。
/** * Creates a thread pool that reuses a fixed number of threads * operating off a shared unbounded queue. At any point, at most * {@code nThreads} threads will be active processing tasks. * If additional tasks are submitted when all threads are active, * they will wait in the queue until a thread is available. * If any thread terminates due to a failure during execution * prior to shutdown, a new one will take its place if needed to * execute subsequent tasks. The threads in the pool will exist * until it is explicitly {@link ExecutorService#shutdown shutdown}. * * @param nThreads the number of threads in the pool * @return the newly created thread pool * @throws IllegalArgumentException if {@code nThreads <= 0} */ public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }
待续。。。
原文:https://www.cnblogs.com/dingpeng9055/p/11350351.html