博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线程池的五种类型
阅读量:4043 次
发布时间:2019-05-24

本文共 3555 字,大约阅读时间需要 11 分钟。

1、SingleThreadExecutor 单线程的线程池

public static ScheduledExecutorService newSingleThreadScheduledExecutor() {        return new DelegatedScheduledExecutorService            (new ScheduledThreadPoolExecutor(1));    }

2、FixedThreadPool固定大小线程池

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

3、ScheduledThreadPool 延迟执行或固定周期执行的线程池

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {        return new ScheduledThreadPoolExecutor(corePoolSize);    }    public ScheduledThreadPoolExecutor(int corePoolSize) {        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,              new DelayedWorkQueue());    }    public ThreadPoolExecutor(int corePoolSize,                              int maximumPoolSize,                              long keepAliveTime,                              TimeUnit unit,                              BlockingQueue
workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); }

4、CachedThreadPool 可变大小的线程池

public static ExecutorService newCachedThreadPool() {        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,                                      60L, TimeUnit.SECONDS,                                      new SynchronousQueue
()); }
上述前四个线程池类型基本都依赖下面的基础函数
/**     * Creates a new {@code ThreadPoolExecutor} with the given initial     * parameters and default thread factory and rejected execution handler.     * It may be more convenient to use one of the {@link Executors} factory     * methods instead of this general purpose constructor.     *     * @param corePoolSize 线程池中的核心线程数     * @param maximumPoolSize 池中最大线程数     * @param keepAliveTime 当线程数超过核心线程数时,这个代表空线程在被终止前最长生存时间     * @param unit 最长存活时间的单位     * @param workQueue 线程池的工作队列,这个队列仅保存被Runnable任务execute方法提交的任务     * @throws IllegalArgumentException 如果下面条件出现会抛出IllegalArgumentException 异常:     *         {@code corePoolSize < 0}
* {@code keepAliveTime < 0}
* {@code maximumPoolSize <= 0}
* {@code maximumPoolSize < corePoolSize} * @throws NullPointerException if {@code workQueue} is null */ public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue
workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); }
只是第一、三个的工作队列有所不同是DelayedWorkQueue,如下所示:
public ScheduledThreadPoolExecutor(int corePoolSize) {        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,              new DelayedWorkQueue());    }

5、WorkStealingPool 采用工作窃取算法队列的线程池

该线程池与前四个不同,适合处理比较耗时的工作任务

/**     * Creates a work-stealing thread pool using all     * {@link Runtime#availableProcessors available processors}     * as its target parallelism level.     * @return the newly created thread pool     * @see #newWorkStealingPool(int)     * @since 1.8     */  public static ExecutorService newWorkStealingPool() {        return new ForkJoinPool            (Runtime.getRuntime().availableProcessors(),             ForkJoinPool.defaultForkJoinWorkerThreadFactory,             null, true);    }

 

转载地址:http://dwodi.baihongyu.com/

你可能感兴趣的文章
yuv to rgb 转换失败呀。天呀。谁来帮帮我呀。
查看>>
yuv420 format
查看>>
YUV420只绘制Y通道
查看>>
yuv420 还原为RGB图像
查看>>
LED恒流驱动芯片
查看>>
驱动TFT要SDRAM做为显示缓存
查看>>
使用file查看可执行文件的平台性,x86 or arm ?
查看>>
qt5 everywhere 编译summary
查看>>
qt5 everywhere编译完成后,找不到qmake
查看>>
交叉编译在x86上调试好的qt程序
查看>>
qt 创建异形窗体
查看>>
可重入函数与不可重入函数
查看>>
简单Linux C线程池
查看>>
内存池
查看>>
输入设备节点自动生成
查看>>
GNU hello代码分析
查看>>
Qt继电器控制板代码
查看>>
wpa_supplicant控制脚本
查看>>
gstreamer相关工具集合
查看>>
RS232 四入四出模块控制代码
查看>>