- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
在这篇文章中,我们将学习Executor的newCachedThreadPool
factory方法。
这个方法创建了一个线程池,它可以根据需要创建新的线程,但是当之前构建的线程可用时,会重新使用。这些线程池通常会提高执行许多短暂的异步任务的程序的性能。
对执行的调用将重用先前构建的线程,如果可用的话。如果没有现有的线程可用,将创建一个新的线程并添加到池中。六十秒内未被使用的线程将被终止并从缓存中删除。因此,一个池子如果保持足够长的空闲时间,就不会消耗任何资源。注意,具有类似属性但细节不同(例如超时参数)的池可以使用ThreadPoolExecutor
constructors创建。
语法。
final ExecutorService executorService = Executors.newCachedThreadPool();
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class CachedThreadPoolExample {
public static void main(final String[] args) throws InterruptedException, ExecutionException {
System.out.println("Thread main started");
Runnable task1 = () -> {
System.out.println("Executing Task1 inside : " + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
};
Runnable task2 = () -> {
System.out.println("Executing Task2 inside : " + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
};
Runnable task3 = () -> {
System.out.println("Executing Task3 inside : " + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
};
final ExecutorService executorService = Executors.newCachedThreadPool();
System.out.println("Submitting the tasks for execution...");
executorService.submit(task1);
executorService.submit(task2);
executorService.submit(task3);
executorService.shutdown();
System.out.println("Thread main finished");
}
}
输出
Thread main started
Submitting the tasks for execution...
Executing Task1 inside : pool-1-thread-1
Executing Task3 inside : pool-1-thread-3
Executing Task2 inside : pool-1-thread-2
Thread main finished
我尝试使用简单的线程来做到这一点并成功了,但我相信使用线程池我可以更有效地完成同样的事情:)?简单线程: public static class getLogFile implements Runna
我想就 Zeller 一年多前发布的同一个问题询问更多细节... javadoc 说 Executors.newCachedThreadPool 返回的服务重用了线程。这怎么可能? 我知道队列结构是如
javadoc 说 Executors.newCachedThreadPool 返回的服务重用了线程。这怎么可能?线程只能通过调用 start 启动一次。那么他们是如何实现的呢?此服务的线程正在无限循
我想记录由 newCachedThreadPool 调用创建的 Activity 线程数。我似乎无法找到从哪里获得这个值。 最佳答案 您可以通过 ThreadFactory至 newCachedThr
我正在为我的应用程序实现一个线程池,我想将 newCachedThreadPool 与 ThreadFactory 一起使用,我想知道我下面写的是否正确。 1.使用newCachedThreadPoo
这个问题在这里已经有了答案: Executors.newCachedThreadPool() versus Executors.newFixedThreadPool() (8 个答案) 关闭 7 年
我做了两个独立的数据库并行读取实现。第一个实现是将 ExecutorService 与 newCachedThreadPool() 构造函数和 Futures 一起使用:我只需调用一个为每个读取案例返
使用Executors.newCachedThreadPool()时,线程池中最初创建了多少个线程,Javadoc没有指定任何数字,是否有一个保证的数字,我们最初总是会得到10个之类的东西.以下文档:
我正在使用 Executors.newCachedThreadPool() 来处理客户端 api 请求。看来,当平均 10tps 流量时,java 进程的驻留内存会随着时间的推移而增长,并且不会减少。
此方法的规范:https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html#newCachedThread
保罗·泰玛 presentation有这一行: Executors.newCacheThreadPool evil, die die die 为什么它是邪恶的? 我会大胆猜测:是不是因为线程数量会无限
在此测试场景中提交的任务(线程)数量也不大。 最佳答案 您需要提供有关如何在池上实例化和调用 submit 的代码示例(IP 在这里应该不是问题,因为我们不需要您的 Callable 类或类似的东西)
我是一名优秀的程序员,十分优秀!