/** * Creates a thread pool that creates new threads as needed, but * will reuse previously constructed threads when they are * available. These pools will typically improve the performance * of programs that execute many short-lived asynchronous tasks. * Calls to {@code execute} will reuse previously constructed * threads if available. If no existing thread is available, a new * thread will be created and added to the pool. Threads that have * not been used for sixty seconds are terminated and removed from * the cache. Thus, a pool that remains idle for long enough will * not consume any resources. Note that pools with similar * properties but different details (for example, timeout parameters) * may be created using {@link ThreadPoolExecutor} constructors. * - 为每一个任务创建一个线程,并且也可以重用已有的线程,无核心线程数量, * - 超过60s的空闲线程将弃用,但是会受到系统实际内存的线程 * * - 当有新任务到来,则插入到SynchronousQueue中,由于SynchronousQueue是同步队列, * - 因此会在池中寻找可用线程来执行,若有可以线程则执行,若没有可用线程则创建一个线程来执行该任务; * - 若池中线程空闲时间超过指定大小,则该线程会被销毁。 * * - 适用:大小无界线程池,执行很多短期异步的小程序或者负载较轻的服务器;或负载较轻的服务器 * * @return the newly created thread pool */ publicstatic ExecutorService newCachedThreadPool(){ returnnew ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
/** * Creates a thread pool that creates new threads as needed, but * will reuse previously constructed threads when they are * available, and uses the provided * ThreadFactory to create new threads when needed. * @param threadFactory the factory to use when creating new threads * @return the newly created thread pool * @throws NullPointerException if threadFactory is null */ publicstatic ExecutorService newCachedThreadPool(ThreadFactory threadFactory){ returnnew ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory); }
/** * 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} * * - 创建可容纳固定数量线程的池子,每隔线程的存活时间是无限的,当池子满了就不在添加线程了; * - 如果池中的所有线程均在繁忙状态,对于新任务会进入阻塞队列中(无界的阻塞队列) * * - 适用:执行长期的任务,性能好很多;为了满足资源管理的需求,而需要限制当前线程的数量;负载比较重的服务器 */ publicstatic ExecutorService newFixedThreadPool(int nThreads){ returnnew ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }
/** * Creates a thread pool that reuses a fixed number of threads * operating off a shared unbounded queue, using the provided * ThreadFactory to create new threads when needed. 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 * @param threadFactory the factory to use when creating new threads * @return the newly created thread pool * @throws NullPointerException if threadFactory is null * @throws IllegalArgumentException if {@code nThreads <= 0} */ publicstatic ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory){ returnnew ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory); }
/** * Creates a thread pool that can schedule commands to run after a * given delay, or to execute periodically. * @param corePoolSize the number of threads to keep in the pool, * even if they are idle * @return a newly created scheduled thread pool * @throws IllegalArgumentException if {@code corePoolSize < 0} * * 创建一个固定核心大小的线程池,最大线程数没有限制, * 线程池内线程存活时间无限制,线程池可以支持定时及周期性任务执行, * 如果所有线程均处于繁忙状态,对于新任务会进入DelayedWorkQueue队列中, * 这是一种按照超时时间排序的队列结构 * * 适用:需要多个后台线程执行周期任务,同时为了满足资源管理的需求而需要限制后台线程的数量的应用场景 */ publicstatic ScheduledExecutorService newScheduledThreadPool(int corePoolSize){ returnnew ScheduledThreadPoolExecutor(corePoolSize); }
/** * Creates a thread pool that can schedule commands to run after a * given delay, or to execute periodically. * @param corePoolSize the number of threads to keep in the pool, * even if they are idle * @param threadFactory the factory to use when the executor * creates a new thread * @return a newly created scheduled thread pool * @throws IllegalArgumentException if {@code corePoolSize < 0} * @throws NullPointerException if threadFactory is null */ publicstatic ScheduledExecutorService newScheduledThreadPool( int corePoolSize, ThreadFactory threadFactory){ returnnew ScheduledThreadPoolExecutor(corePoolSize, threadFactory); }
/** * Creates a single-threaded executor that can schedule commands * to run after a given delay, or to execute periodically. * (Note however that if this single * thread terminates due to a failure during execution prior to * shutdown, a new one will take its place if needed to execute * subsequent tasks.) Tasks are guaranteed to execute * sequentially, and no more than one task will be active at any * given time. Unlike the otherwise equivalent * {@code newScheduledThreadPool(1)} the returned executor is * guaranteed not to be reconfigurable to use additional threads. * @return the newly created scheduled executor * - 适用于需要单个后台线程执行周期任务,同时需要保证顺序地执行各个任务的应用场景。 */ publicstatic ScheduledExecutorService newSingleThreadScheduledExecutor(){ returnnew DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1)); }
/** * Creates a single-threaded executor that can schedule commands * to run after a given delay, or to execute periodically. (Note * however that if this single thread terminates due to a failure * during execution prior to shutdown, a new one will take its * place if needed to execute subsequent tasks.) Tasks are * guaranteed to execute sequentially, and no more than one task * will be active at any given time. Unlike the otherwise * equivalent {@code newScheduledThreadPool(1, threadFactory)} * the returned executor is guaranteed not to be reconfigurable to * use additional threads. * @param threadFactory the factory to use when creating new * threads * @return a newly created scheduled executor * @throws NullPointerException if threadFactory is null */ publicstatic ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory){ returnnew DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1, threadFactory)); }
/** * Creates an Executor that uses a single worker thread operating * off an unbounded queue. (Note however that if this single * thread terminates due to a failure during execution prior to * shutdown, a new one will take its place if needed to execute * subsequent tasks.) Tasks are guaranteed to execute * sequentially, and no more than one task will be active at any * given time. Unlike the otherwise equivalent * {@code newFixedThreadPool(1)} the returned executor is * guaranteed not to be reconfigurable to use additional threads. * * @return the newly created single-threaded Executor * * 创建只有一个线程的线程池,且线程的存活时间是无限的; * 当该线程正繁忙时,对于新任务会进入阻塞队列中(无界的阻塞队列) * * 适用:一个任务一个任务执行的场景(顺序执行);并且在任意时间点,不会有多个线程是活动的场景 */ publicstatic ExecutorService newSingleThreadExecutor(){ returnnew FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); }
/** * Creates an Executor that uses a single worker thread operating * off an unbounded queue, and uses the provided ThreadFactory to * create a new thread when needed. Unlike the otherwise * equivalent {@code newFixedThreadPool(1, threadFactory)} the * returned executor is guaranteed not to be reconfigurable to use * additional threads. * * @param threadFactory the factory to use when creating new * threads * * @return the newly created single-threaded Executor * @throws NullPointerException if threadFactory is null */ publicstatic ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory){ returnnew FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory)); }