解读线程池-Executors的newSingleThreadExecutor和newFixedThreadPool(1)区别
目录
- 线程池Executors的newSingleThreadExecutor和newFixedThreadPool(1)
- 与其他等效的newFixedThreadPool(1)不同
- 与其他等效的newScheduledThreadPool(1)不同
- 举个例子
- 总结
线程池Executors的newSingleThreadExecutor和newFixedThreadPool(1)
与其他等效的newFixedThreadPool(1)不同
- newSingleThreadExecutor返回的执行器保证不可重新配置。
与其他等效的newScheduledThreadPool(1)不同
- newSingleThreadScheduledExecutorhttp://www.devze.com返回的执行器保证不可重新配置以使用其他线程。
newFixedThreadPool(1)的返回结果我们可以通过强转变成ThreadPoolExecutor,但是这个类是可以自行指定线程数的。
我们可以通过setCorePoolSize方法来修改。
这样也就是说,这两个方法的最大的区别是第一个方法可以修改线程的数量,如果用来指定线程数量为1是不安全的。
newSingleThreadExecutor方法则通过提供了一个包装类完全堵住了这个漏洞。
举个例子
拿newSingleThreadExecutor和newFixedThreadPool(1)举例
import org.junit.Test;
import Java.util.concurrent.ExecutorService;
ipythonmport java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolE编程xecutor;
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-threadandroid-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)
*/
}
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
加载中,请稍侯......
精彩评论