• Android随笔-线程池


    线程池-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.corePoolSize = corePoolSize;
            this.maximumPoolSize = maximumPoolSize;
            this.workQueue = workQueue;
            this.keepAliveTime = unit.toNanos(keepAliveTime);
            this.threadFactory = threadFactory;
            this.handler = handler;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • corePoolSize,核心线程数。
    • maximumPoolSize,线程池最大线程数。
    • keepAliveTime,非核心线程闲置时存活时间。
    • unit,非核心线程闲置存活时间单位,有天、时、分、秒、微妙、纳秒、毫秒。
    • workQueue,等待队列,当任务大于核心线程数,且没有到达被丢弃的程度,任务会被加入任务队列中。
    • threadFactory,线程工厂,在工厂中创建线程。
    • defaultHandler,拒绝执行任务策略,比如工作队列已满,最大线程已满就会拒绝执行新任务。defaultHandler是RejectedExecutionHandler类型,拒绝策略有四种:
      1. CallerRunsPolicy:只要线程池未关闭就会运行丢弃的任务,任务不会丢失,但是会影响性能。
      2. AbortPolicy(默认):直接抛出RejectedExecution-Exception。
      3. DiscardPolicy:直接丢弃任务。
      4. DiscardOldestPolicy:丢弃阻塞队列中靠最前的任务,并执行当前任务。

    执行流程

    1. 未达核心数,直接启动一个线程。‘
    2. 超过核心数,加入队列等待执行。
    3. 队列已满,未达最大线程数,启动非核心线程。
    4. 队列已满,已达最大线程数,拒绝执行。

    作用

    1. 重用线程池中的线程,避免因为线程的创建和销毁带来性能开销。
    2. 有效控制线程池的最大并发数,避免大量线程之间因为抢占系统资源而导致阻塞现象。
    3. 可以对线程进行一些管理,可以执行定时任务,以及指定间隔时间的循环任务等。

    分类

    FixedThreadPool

        public static ExecutorService newFixedThreadPool(int nThreads) {
            return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
        }
    
    • 1
    • 2
    • 3
    • 线程数量固定。
    • 线程空闲时,线程不会被回收,除非线程池关闭。
    • 只有核心线程,没有超时限制,任务队列没有大小限制(最大Integer.MAX_VALUE)。

    CachedThreadPool

        public static ExecutorService newCachedThreadPool() {
            return new ThreadPoolExecutor(0, 2147483647, 60L, TimeUnit.SECONDS, new SynchronousQueue());
        }
    
    • 1
    • 2
    • 3
    • 线程数量不固定。
    • 只有非核心线程,最大为Integer.MAX_VALUE。
    • 超时为60s。
    • 任务队列为空集合。
    • 适合大量的耗时较少的任务。
    • 线程池闲置时,所有线程都会因为超时而停止,实际上此时没有任何线程,不占用任何资源。

    ScheduledThreadPool

        public ScheduledThreadPoolExecutor(int corePoolSize) {
            super(corePoolSize, 2147483647, 10L, TimeUnit.MILLISECONDS, new ScheduledThreadPoolExecutor.DelayedWorkQueue());
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 核心固定,非核心没有限制,非核心只要空闲就会立即被回收。
    • 适合执行定时任务,或固定周期的重复任务。

    SingleThreadPool

        public static ExecutorService newSingleThreadExecutor() {
            return new Executors.FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()));
        }
    
    • 1
    • 2
    • 3
    • 只有一个核心线程,所有任务按顺序执行。
    • 不需要处理线程同步问题
  • 相关阅读:
    UE4 回合游戏项目 09- 添加人物属性值
    MySQL总结(DDL、DML、TPL、DCL)
    Spark基础
    [好题][思维]Paimon Sorting 2021年ICPC南京站D
    useEffect(fn, []) 不等于 componentDidMount()
    鸿蒙App动画、弹窗
    【6 ElementUI Tabs控件第二个tab页签Div宽度缩小的问题】
    OpenCV:08图像金字塔
    Object.defineProperty也能监听数组变化?
    SQL Server如何新建作业
  • 原文地址:https://blog.csdn.net/qq_34202054/article/details/126695153