首页 > 其他 > 详细

Executor框架

时间:2019-08-14 11:27:19      阅读:73      评论:0      收藏:0      [点我收藏+]

Executor框架的主要成员:ThreadPoolExecutor、ScheduledThreadPoolExecutor、Future接口、runnable接口、Callable接口和Executors。

ThreadPoolExecutor 

通常使用工厂类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>());
    }

 

待续。。。

 

Executor框架

原文:https://www.cnblogs.com/dingpeng9055/p/11350351.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!