Java类Executors详解

版权声明:如若转载,请联系作者。 https://blog.csdn.net/liu16659/article/details/83956148

Java类Executors详解【待完善】

1.类简介

 * Factory and utility methods for {@link Executor}, {@link
 * ExecutorService}, {@link ScheduledExecutorService}, {@link
 * ThreadFactory}, and {@link Callable} classes defined in this
 * package. This class supports the following kinds of methods:

针对定义在这个包中的 Executor,ExecutorService,ScheduledExecutorService ,ThreadFactory,Callable 等类的工厂/实用方法。这个类支持如下几种方法:

  • Methods that create and return an {@link ExecutorService} set up with commonly useful configuration settings. 使用常用且有用的配置去创建并返回一个 ExecutorService

  • Methods that create and return a {@link ScheduledExecutorService} set up with commonly useful configuration settings. 使用常用配置返回一个 ScheduledExecutorService

  • Methods that create and return a “wrapped” ExecutorService, that disables reconfiguration by making implementation-specific methods inaccessible. 创建并返回一个"wrapped" ExecutorService,这使重配置无效,通过实现具体的,不可访问的方法。

  • Methods that create and return a {@link ThreadFactory} that sets newly created threads to a known state. 创建并返回一个ThreadFactory,这个可以设置最近创建的线程到一个指定的状态。

  • Methods that create and return a {@link Callable} out of other closure-like forms, so they can be used in execution methods requiring {@code Callable}.

2.方法介绍

  • newFixedThreadPool
 /**
     * 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>());
    }

该方法的功能就是:创建一个线程池,可用于复用一个固定数量的线程,操作一个共享的很大的(任务)队列。

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/83956148