例子:

ExecutorService es = Executors.newFixedThreadPool(5);
try {
for (int i = 0; i < 20; i++) {
Runnable syncRunnable = new Runnable() {
@Override
public void run() {
log.info(Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
es.execute(syncRunnable);
}
} finally {
es.shutdown();
}
运行结果:
* 10:20:14.630 pool-1-thread-5
10:20:14.630 pool-1-thread-1
10:20:14.632 pool-1-thread-4
10:20:14.630 pool-1-thread-2
10:20:14.630 pool-1-thread-3
10:20:16.635 pool-1-thread-4
10:20:16.635 pool-1-thread-5
10:20:16.635 pool-1-thread-1
10:20:16.636 pool-1-thread-2
10:20:16.637 pool-1-thread-3
10:20:18.637 pool-1-thread-3
10:20:18.638 pool-1-thread-4
10:20:18.641 pool-1-thread-5
10:20:18.641 pool-1-thread-1
10:20:18.642 pool-1-thread-2
10:20:20.638 pool-1-thread-3
10:20:20.639 pool-1-thread-4
10:20:20.641 pool-1-thread-5
10:20:20.642 pool-1-thread-1
10:20:20.642 pool-1-thread-2

调用的ThreadPoolExecutor:

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

corePoolSize=maximumPoolSize=5

keepAliveTime=0

allowCoreThreadTimeout=false(默认)

因此,

  • 线程池中的线程数永远是5,永久存活。
  • 对于新任务,当队列未满时,插入队列;当队列已满时,默认执行AbortPolicy,即抛出异常。
  • 支持线程reuse

线程池(2)Executors.newFixedThreadPool的更多相关文章

  1. 线程池之 Executors

    线程池之 Executors + 面试题 线程池的创建分为两种方式:ThreadPoolExecutor 和 Executors,上一节学习了 ThreadPoolExecutor 的使用方式,本节重 ...

  2. 【转】一次Java线程池误用(newFixedThreadPool)引发的线上血案和总结

    [转]原文链接:https://cloud.tencent.com/developer/article/1497826 这是一个十分严重的线上问题 自从最近的某年某月某天起,线上服务开始变得不那么稳定 ...

  3. 一次Java线程池误用(newFixedThreadPool)引发的线上血案和总结

    一次Java线程池误用(newFixedThreadPool)引发的线上血案和总结 这是一个十分严重的线上问题 自从最近的某年某月某天起,线上服务开始变得不那么稳定(软病).在高峰期,时常有几台机器的 ...

  4. 线程池工厂方法newFixedThreadPool()和newCachedThreadPool()

    newFixedThreadPool()方法: 该方法返回一个固定数量的线程池,当一个新的任务提交时,线程池中若有空闲线程,则立即执行. 若没有.则新的任务被暂存在一个任务队列中,待线程空闲时,便处理 ...

  5. 线程池工厂Executors编程的艺术

    Executors是一个线程池的工厂类,提供各种有用的线程池的创建,使用得当,将会使我们并发编程变得简单!今天就来聊聊这个工厂类的艺术吧! Executors只是Executor框架的主要成员组件之一 ...

  6. Java线程池ThreadPoolExecutor&&Executors

    一.先看看传统的开启线程. new Thread(new Runnable() { @Override public void run() { } }).start(); 缺点: 1.每次new Th ...

  7. 线程池(2)-Executors提供4个线程池

    1.为什么不使用Executors提供4个线程池创建线程池 阿里巴巴开放手册这样写: . [强制]线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式, ...

  8. Java并发包线程池之Executors、ExecutorCompletionService工具类

    前言 前面介绍了Java并发包提供的三种线程池,它们用处各不相同,接下来介绍一些工具类,对这三种线程池的使用. Executors Executors是JDK1.5就开始存在是一个线程池工具类,它定义 ...

  9. Executors、ThreadPoolExecutor线程池讲解

    官方+白话讲解Executors.ThreadPoolExecutor线程池使用 Executors:JDK给提供的线程工具类,静态方法构建线程池服务ExecutorService,也就是Thread ...

  10. java线程API学习 线程池ThreadPoolExecutor(转)

    线程池ThreadPoolExecutor继承自ExecutorService.是jdk1.5加入的新特性,将提交执行的任务在内部线程池中的可用线程中执行. 构造函数 ThreadPoolExecut ...

随机推荐

  1. legend2---开发日志15(功能需求明确,设计好类和结构的好处是)

    legend2---开发日志15(功能需求明确,设计好类和结构的好处是) 一.总结 一句话总结: 极快简化编程,节约大量时间 1.多个类型的物品,比如商店和寻宝的丹药,装备,特性书,英雄石等等 应该怎 ...

  2. 分享知识-快乐自己:论Hibernate中的缓存机制

    Hibernate缓存 缓存: 是计算机领域的概念,它介于应用程序和永久性数据存储源之间. 缓存: 一般人的理解是在内存中的一块空间,可以将二级缓存配置到硬盘.用白话来说,就是一个存储数据的容器.我们 ...

  3. packettotal.com - PacketTotal - A Free Online PCAP Analysis Engine

    找相似的pcap文件:https://packettotal.com/app/analysis?id=aff96903aae4b73364e027c8a3e85060&name=similar ...

  4. L87

    Fear Makes Art More Engaging Emmanuel Kant spoke often about the sublime, and specifically how art b ...

  5. (转)linux 打开文件数 too many open files 解决方法

    too many open files 出现这句提示的原因是程序打开的文件/socket连接数量超过系统设定值. 查看每个用户最大允许打开文件数量 ulimit -a fdipzone@ubuntu: ...

  6. PG替换字段中的回车与换行

    REPLACE(filed, CHR(10), '') //替换换行符 REPLACE(filed, CHR(13), '') //替换回车符

  7. AtCoder Grand Contest 013 C:Ants on a Circle

    题目传送门:https://agc013.contest.atcoder.jp/tasks/agc013_c 题目翻译 给你一个周长为\(L\)的圆,有\(N\)只蚂蚁在圆上爬,速度为一单位距离每秒. ...

  8. idea救命篇--误删文件恢复

    删除.覆盖文件恢复:右键文件--Local History 查到被删的代码,idea自动保存的. 即使文件目录文件被删了,在同地方新建一个同名空文件,也可以通过Local History找回来代码.

  9. (六)编写基类BaseDao

    在action中继承了ActionSupport和其它一些公共属性,如selectedRow等:可能以后还会产生更多公共的内容,所以应该把这些共有的抽取出来,放入到一个基本action中,我们命名为B ...

  10. glib 库 hash table 使用

    glib库提供了 hashtable 的实现 1. 常用函数: 创建一个 GHashTable 函数: hash_func 是创建value的key值的函数,key_equal_func 是比较两个k ...