Executor的原理是什么

发布时间:2021-06-18 17:18:23 作者:Leah
来源:亿速云 阅读:261

这篇文章给大家介绍Executor的原理是什么,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

一、Executor 线程池体系介绍

1. Executor 框架体系介绍

Executor的原理是什么

2. ThreadPoolExecutor 源码解析

ThreadPoolExecutor有多个重载的构造方法,我们基于最完整的构造方法来分析每个参数的作用。

public ThreadPoolExecutor(int corePoolSize,		//核心线程池数量
                              int maximumPoolSize,	//最大线程池数量
                              long keepAliveTime,	//线程数大于核心线程池数,空闲线程的最大存活时间
                              TimeUnit unit,		//参数的时间单位
                              BlockingQueue<Runnable> workQueue,	//线程等待队列
                              ThreadFactory threadFactory,		//用于设置创建线程的工厂
                              RejectedExecutionHandler handler) {	//设置拒绝策略
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.acc = System.getSecurityManager() == null ?
                null :
                AccessController.getContext();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

二、Executors 线程池工具类

1. newFixedThreadPool: 固定大小线程池

public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>(),
                                      threadFactory);
    }

2. newSingleThreadExecutor: 单线程化线程池

public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>(),
                                    threadFactory));
    }

3. newCachedThreadPool: 可缓存线程池

public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>(),
                                      threadFactory);
    }

4. newScheduledThreadPool: 周期性线程池

public static ScheduledExecutorService newScheduledThreadPool(
            int corePoolSize, ThreadFactory threadFactory) {
        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    }

5. newWorkStealingPool: 工作窃取线程池(jdk1.8)

public static ExecutorService newWorkStealingPool(int parallelism) {
        return new ForkJoinPool
            (parallelism,
             ForkJoinPool.defaultForkJoinWorkerThreadFactory,
             null, true);
    }

关于Executor的原理是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. executor启动task
  2. Spark任务的core,executor,memory资源配置方法是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

executor

上一篇:快速解码UNICODE/UTF8编码的方法

下一篇:python清洗文件中数据的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》