例子:

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. SpringMVC框架<mvc:default-servlet-handler/>的作用

    1.创建一个新工程 Eclipse下新建一个web项目,File>New>Dynamic Web Project     2.添加Jar包     3.配置Web.xml     4.配置 ...

  2. L97

    We are young. So let's set the world on fire. We can burn brighter than the sun.我们是青年,让我们点亮世界,释放生命,胜 ...

  3. linux命令学习笔记-eval命令详解

    功能说明:重新运算求出参数的内容. 语 法:eval [参数] 补充说明:eval可读取一连串的参数,然后再依参数本身的特性来执行. 参 数:参数不限数目,彼此之间用分号分开. .eval命令将会首先 ...

  4. java自定义类型 作为HashMap中的Key值 (Pair<V,K>为例)

    由于是自定义类型,所以HashMap中的equals()方法和hashCode()方法都需要自定义覆盖. 不然内容相同的对象对应的hashCode会不同,无法发挥算法的正常功能,覆盖equals方法, ...

  5. 2084. Asm.Def的基本算法

    2084. Asm.Def的基本算法 传送门 ★☆   输入文件:asm_algo.in   输出文件:asm_algo.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] ...

  6. 洛谷P1525关押罪犯——二分做法

    题目:https://www.luogu.org/problemnew/show/P1525 二分答案,二分图染色判断是否可行. 代码如下: #include<iostream> #inc ...

  7. Scala学习——类,继承,接口(中)

    基本类的使用:(初) package com.dtspark.scala.basics /** * trait是一个接口 * 接口的第一次继承用extends,多继承时用with * 多继承时,如果这 ...

  8. Spring boot 学习 九

    一:经过试验发现,如果使用如下的Controller(@RequestBody), 前台POST的请求body只能是JSON,如果是form-data, X-www-form-urlencoded 或 ...

  9. tp框架下,数据库和编辑器都是utf-8, 输出中文却还是乱码

    输出: array(2) { [0]=> array(4) { ["id"]=> string(1) "1" ["user"]= ...

  10. query builder 在线生成sql

    http://devtools.korzh.com/easyquery/javascript/docs/javascript-query-builder-php