zoukankan      html  css  js  c++  java
  • 线程池的5种创建方式

    1. Single Thread Executor:只有一个线程的线程池,因此所有提交的任务是顺序执行。

    代码:Executors.newSingleThreadExecutor()

    2. Cached Thread Pool:线程池里有很多线程需要同时执行,老的可用线程将被新的任务触发重新执行,如果线程超过60秒内没执行,那么将被终止并从池中删除。

    代码:Executors.newCachedThreadPool()

    3. Fixed Thread Pool:拥有固定线程数的线程池,如果没有任务执行,那么线程会一直等待。

    代码:Executors.newFixedThreadPool(4)

    在构造函数中的参数4是线程池的大小,你可以随意设置,也可以和cpu的核数量保持一致,获取cpu的核数量方式:int cpuNums = Runtime.getRuntime().availableProcessors();

    4. Scheduled Thread Pool:用来调度即将执行的任务的线程池。

    代码:Executors.newScheduledThreadPool()

    5. Single Thread Scheduled Pool:只有一个线程,用来调度任务在指定时间执行。

    代码:Executors.newSingleThreadScheduledExecutor()

  • 相关阅读:
    2020春Contest
    HDU Count the string (KMP)
    P1757 通天之分组背包
    L1-050 倒数第N个字符串
    3月份目标
    Division UVa725
    数三角
    luogu P2051 [AHOI2009]中国象棋 dp 状态压缩+容斥
    Codeforces Round #654 (Div. 2) E
    Codeforces Round #654 (Div. 2) D
  • 原文地址:https://www.cnblogs.com/fjlcoding/p/10070730.html
Copyright © 2011-2022 走看看