美文网首页
线程池核心参数

线程池核心参数

作者: 陈桐Caliburn | 来源:发表于2020-07-03 14:45 被阅读0次

线程池核心参数

    ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler)

1)corePoolSize(线程池基本大小)

2)maximumPoolSize(线程池最大数量)

3)keepAliveTime(空闲线程活动保持时间):线程池工作线程空闲后,保持存活时间

4)TimeUnit(时间单位)

5)workQueue(任务队列):用于保存等待执行任务的阻塞队列
1、ArrayBlockQueue
2、LinkedBlockQueue
3、SynchronousQueue(不存储元素阻塞队列)
4、PriorityBlockingQueue(优先级无限阻塞队列)

6) threadFactory(线程工厂)

7) handler(饱和策略)
1、AbortPolicy:直接抛出异常
2、CallerRunsPolicy:调用者所在线程来运行任务
3、DiscardOrderestPolicy:丢队列最近一个任务,并执行
4、DiscardPolicy:不处理,丢掉

四种线程池

固定线程池 newFixedThreadPool
缓存线程池 newCachedThreadPool
单个线程 newSingleThreadExecutor
定时线程池 newScheduledThreadPool

    //固定线程池
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor( nThreads, nThreads,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>() );
    }

    //缓存线程池
    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor( 0, Integer.MAX_VALUE,
                60L, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>() );
    }


    //单个线程池
    public static ExecutorService newSingleThreadExecutor() {
        return new ThreadPoolExecutor( 1, 1,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>() );
    }

    //定时线程池
    public static ExecutorService newScheduledThreadPoolExecutor(int corePoolSize) {
        return new ThreadPoolExecutor( corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
                new ScheduledThreadPoolExecutor.DelayedWorkQueue() );
    }

任务性质分类

获得cpu个数

        int cpu = Runtime.getRuntime().availableProcessors();

CPU密集型:Ncpu+1
IO密集型:2* Ncpu
混合型:线程池分解

示例代码pool

https://github.com/yinlingchaoliu/juc.git

相关文章

  • ThreadPool

    线程池核心参数 corePoolSize: int 核心线程数 线程池初始化后,线程池中没有任何线程,线程池会等待...

  • Android 中的线程池

    线程池核心参数 核心线程数 corePoolSize线程数容量 maximumPoolSize非核心线程被回收...

  • 线程池核心参数

    线程池核心参数 1)corePoolSize(线程池基本大小) 2)maximumPoolSize(线程池最大数量...

  • 掌握线程池7大核心参数,自己也可以手写线程池

    手写线程池只需了解7个线程池核心参数 参数名中文名说明corePoolSize核心线程数默认不会销毁,需要设置al...

  • 自定义注解实现一个可配置线程池

    前言 PoolConfig(线程池核心配置参数): ThreadPoolConfig(线程池配置 yml/pope...

  • 2019-03-13 自定义连接池

    连接池:即线程池要自定义先要了解线程池模型,即线程池的核心参数1.coresize核心线程池,即运行的线程2.ma...

  • Android 线程池原理

    线程池核心类 : ThreadPoolExecutor:提供了一系列参数来配置线程池 线程池优点: 1.重用线程池...

  • 线程池概述

    为什么要使用线程池? 线程池核心参数 线程池的几种拒绝策略 execute()和submit()的区别 线程池工作...

  • 线程池初探

    线程池架构图 线程池状态流转图 线程池主要参数介绍 corePoolSize: 核心线程数量 maximumPoo...

  • 线程池原理

    线程池关键参数 核心线程数corePoolSize:线程池维护线程最少数量最大线程数 maxPollSize:线程...

网友评论

      本文标题:线程池核心参数

      本文链接:https://www.haomeiwen.com/subject/gothqktx.html