解读线程池-Executors的newSingleThreadExecutor和newFixedThreadPool(1)区别
更新时间:2024年08月06日 14:46:35 作者:Ahuuua
这篇文章主要介绍了解读线程池-Executors的newSingleThreadExecutor和newFixedThreadPool(1)区别,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
线程池Executors的newSingleThreadExecutor和newFixedThreadPool(1)
与其他等效的newFixedThreadPool(1)不同
- newSingleThreadExecutor返回的执行器保证不可重新配置。
与其他等效的newScheduledThreadPool(1)不同
- newSingleThreadScheduledExecutor返回的执行器保证不可重新配置以使用其他线程。
newFixedThreadPool(1)的返回结果我们可以通过强转变成ThreadPoolExecutor,但是这个类是可以自行指定线程数的。
我们可以通过setCorePoolSize方法来修改。
这样也就是说,这两个方法的最大的区别是第一个方法可以修改线程的数量,如果用来指定线程数量为1是不安全的。
newSingleThreadExecutor方法则通过提供了一个包装类完全堵住了这个漏洞。
举个例子
拿newSingleThreadExecutor和newFixedThreadPool(1)举例
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class ThreadPoolDemo {
static class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("开始");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("结束" + Thread.currentThread().getName());
}
}
@Test
public void test() throws InterruptedException {
//创建Runnable实例对象
MyRunnable r = new MyRunnable();
//创建线程池对象
System.out.println("fixedThreadPool");
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(1);//包含1个线程对象
for (int i = 0; i < 10; i++) {
fixedThreadPool.submit(r);
}
Thread.sleep(10000);
/**
* 以上输出:
* fixedThreadPool
* 开始
* 结束pool-1-thread-1
* 开始
* 结束pool-1-thread-1
* 开始
* 结束pool-1-thread-1
* 开始
* 结束pool-1-thread-1
* 开始
* 结束pool-1-thread-1
* 开始
* 结束pool-1-thread-1
* 开始
* 结束pool-1-thread-1
* 开始
* 结束pool-1-thread-1
* 开始
* 结束pool-1-thread-1
* 开始
* 结束pool-1-thread-1
*/
System.out.println("singleThreadExecutor");
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
singleThreadExecutor.submit(r);
}
Thread.sleep(10000);
/**
* 以上输出:
* singleThreadExecutor
* 开始
* 结束pool-2-thread-1
* 开始
* 结束pool-2-thread-1
* 开始
* 结束pool-2-thread-1
* 开始
* 结束pool-2-thread-1
* 开始
* 结束pool-2-thread-1
* 开始
* 结束pool-2-thread-1
* 开始
* 结束pool-2-thread-1
* 开始
* 结束pool-2-thread-1
* 开始
* 结束pool-2-thread-1
* 开始
* 结束pool-2-thread-1
*/
System.out.println("fixedThreadPool(5)");
((ThreadPoolExecutor) fixedThreadPool).setCorePoolSize(5);
for (int i = 0; i < 10; i++) {
fixedThreadPool.submit(r);
}
Thread.sleep(10000);
/**
* 以上输出:
* fixedThreadPool(5)
* 开始
* 开始
* 开始
* 开始
* 开始
* 结束pool-1-thread-1
* 结束pool-1-thread-5
* 结束pool-1-thread-2
* 结束pool-1-thread-3
* 结束pool-1-thread-4
* 开始
* 结束pool-1-thread-4
* 开始
* 结束pool-1-thread-4
* 开始
* 结束pool-1-thread-4
* 开始
* 结束pool-1-thread-4
* 开始
* 结束pool-1-thread-4
* singleThreadExecutor(5)
*/
System.out.println("singleThreadExecutor(5)");
((ThreadPoolExecutor) singleThreadExecutor).setCorePoolSize(5);
for (int i = 0; i < 10; i++) {
singleThreadExecutor.submit(r);
}
/**
* 以下输出:
* Exception in thread "main" java.lang.ClassCastException: java.util.concurrent.Executors$FinalizableDelegatedExecutorService cannot be cast to java.util.concurrent.ThreadPoolExecutor
* at ThreadPoolDemo.main(ThreadPoolDemo.java:33)
*/
}
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Spring Boot集成MyBatis实现通用Mapper的配置及使用
关于MyBatis,大部分人都很熟悉。MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。这篇文章主要介绍了Spring Boot集成MyBatis实现通用Mapper,需要的朋友可以参考下2018-08-08
java面试散列表及树所对应容器类及HashMap冲突解决全面分析
这篇文章主要介绍了java面试中的java散列表及树所对应容器类与HashMap冲突解决的问题总结,有需要的朋友可以借鉴参考下,希望能够有所帮助2021-10-10
mybatis查询返回Map<String,Object>类型的讲解
这篇文章主要介绍了mybatis查询返回Map<String,Object>类型的讲解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-06-06


最新评论