第六章JavanewCachedThreadPool示例|
来源:网络
编辑:雨凉
时间:2022-06-19
在本教程中,我们将学习 Executor 的 newCachedThreadPool 工厂方法。 在上一篇教程中,我分享了 ThreadPoolExecutor 的介绍。如果您不了解 ThreadPoolExecutor 的概念,您应该先了解一下。
Executor 的 newCachedThreadPool 工厂方法:
此方法返回一个无界线程池。它将最大池大小设置为 Integer.Max,它将根据需要创建新线程。如果需求减少,如果线程空闲超过 1 分钟,它将关闭线程。
例子:
让我们创建一个任务。这里的任务是读取不同的文件并处理它们。
package org.arpit.java2blog.bean;
public class FetchDataFromFile implements Runnable{
private final String fileName;
public FetchDataFromFile(String fileName) {
super();
this.fileName = fileName;
}
@Override
public void run() {
try {
System.out.println("Fetching data from "+fileName+" by "+Thread.currentThread().getName());
Thread.sleep(5000); // Reading file
System.out.println("Read file successfully: "+fileName+" by "+Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public String getFileName() {
return fileName;
}
}
让我们创建 ThreadPoolExecutor 它将消耗上述任务并处理它。
package org.arpit.java2blog;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class ThreadPoolExecutorMain {
public static void main(String args[]) {
// Getting instance of ThreadPoolExecutor using Executors.newCachedThreadPool factory method
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
for (int i = 1; i <= 10; i++) {
FetchDataFromFile fdff = new FetchDataFromFile("File :" + i);
System.out.println("A new file has been added to read : " + fdff.getFileName());
threadPoolExecutor.execute(fdff);
}
threadPoolExecutor.shutdown();
}
}
当你运行上面的程序时,你会得到下面的输出:
A new file has been added to read : File :1
A new file has been added to read : File :2
Fetching data from File :1 by pool-1-thread-1
Fetching data from File :2 by pool-1-thread-2
A new file has been added to read : File :3
A new file has been added to read : File :4
Fetching data from File :3 by pool-1-thread-3
Fetching data from File :4 by pool-1-thread-4
A new file has been added to read : File :5
Fetching data from File :5 by pool-1-thread-5
A new file has been added to read : File :6
Fetching data from File :6 by pool-1-thread-6
A new file has been added to read : File :7
Fetching data from File :7 by pool-1-thread-7
A new file has been added to read : File :8
A new file has been added to read : File :9
Fetching data from File :8 by pool-1-thread-8
A new file has been added to read : File :10
Fetching data from File :9 by pool-1-thread-9
Fetching data from File :10 by pool-1-thread-10
如果你注意到,我们已经提交了 10 个任务,它根据需求创建了 10 个新线程。如果任何线程空闲超过一分钟,它就会将其拆除。当您想要比 newFixedThreadPool 更好的排队性能时,newCachedThreadPool 是一个不错的选择。如果要限制资源管理的并发任务数,请使用 newFixedThreadPool。
相关文章:
- [其它]第六章JavaFutureTask示例|
- [其它]第六章JavaScheduledThreadPoolExecutor示例|
- [其它]第六章使用Callable和Future的JavaExecutorService示例|
- [其它]第六章JavanewFixedThreadPool示例|
- [其它]第六章:为什么我们需要Executor框架?|
- [其它]第五章在java中使用3个线程打印序列|
- [其它]第五章java中notify和notifyAll的区别|
- [其它]第五章我们可以在java中启动一个线程两次吗|
- [其它]第五章java中的对象级锁定与类级锁定|
- [其它]第五章Java线程连接示例|
相关推荐:
- [其它]第三章java中的抽象类|
- [其它]第四章java中的TreeMap与示例|
- [其它]第四章java中的hashcode()和equals()方法|
- [其它]第六章JavaFutureTask示例|
- [其它]计算机组成原理PDF扫描格式电子版百度云网盘下载
- [其它]程序设计语言概念(第9版)[Robert W. Sebesta]pdf格式电子版百度云网盘下载[53.8M]
- [其它]程序员必读之软件架构[Simon Brown]pdf格式电子版百度云网盘下载[41.6M]
- [其它]程序员的思维修炼 开发认知潜能的九堂课[Andy Hunt]pdf格式电子版百度云网盘下载
- [其它]程序员的职业素养[Robert C. Martin]pdf格式电子版百度云网盘下载[21.6M]
- [其它]程序员教程(第3版)[张淑平]pdf格式电子版百度云网盘下载[136.5M]
栏目分类
最新文章
热门文章
- 计算机组成原理PDF扫描格式电子版百度云网盘下载
- 硅谷之谜[吴军]pdf格式电子版百度云网盘下载[59.5M]
- 自选基金助手,一款Chrome扩展程序
- 七周七语言 理解多种编程范型[Bruce A. Tate]pdf格式电子版百度云网盘下载[9M]
- 系统集成项目管理工程师考试考眼分析与样卷解析(2014版)[软考新大纲研究组]
- 程序员面试金典(第5版)[Gayle Laakmann McDowell]pdf格式电子版百度云网盘下载[81.5M]
- 创业维艰 如何完成比难更难的事[Ben Horowitz]pdf格式电子版百度云网盘下载[65.3
- 程序员的思维修炼 开发认知潜能的九堂课[Andy Hunt]pdf格式电子版百度云网盘下载
- 图灵的秘密 他的生平、思想及论文解读[Charles Petzold]pdf格式电子版百度云网盘下
- OCP OCA认证考试指南全册:Oracle Database 11g(1Z0-051、1Z0-052、1Z0-053)[John Watson]pdf格式