解讀線程池-Executors的newSingleThreadExecutor和newFixedThreadPool(1)區(qū)別
線程池Executors的newSingleThreadExecutor和newFixedThreadPool(1)
與其他等效的newFixedThreadPool(1)不同
- newSingleThreadExecutor返回的執(zhí)行器保證不可重新配置。
與其他等效的newScheduledThreadPool(1)不同
- newSingleThreadScheduledExecutor返回的執(zhí)行器保證不可重新配置以使用其他線程。
newFixedThreadPool(1)的返回結果我們可以通過強轉變成ThreadPoolExecutor,但是這個類是可以自行指定線程數(shù)的。
我們可以通過setCorePoolSize方法來修改。
這樣也就是說,這兩個方法的最大的區(qū)別是第一個方法可以修改線程的數(shù)量,如果用來指定線程數(shù)量為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 {
//創(chuàng)建Runnable實例對象
MyRunnable r = new MyRunnable();
//創(chuàng)建線程池對象
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)
*/
}
}
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring Boot集成MyBatis實現(xiàn)通用Mapper的配置及使用
關于MyBatis,大部分人都很熟悉。MyBatis 是一款優(yōu)秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。這篇文章主要介紹了Spring Boot集成MyBatis實現(xiàn)通用Mapper,需要的朋友可以參考下2018-08-08
java面試散列表及樹所對應容器類及HashMap沖突解決全面分析
這篇文章主要介紹了java面試中的java散列表及樹所對應容器類與HashMap沖突解決的問題總結,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
mybatis查詢返回Map<String,Object>類型的講解
這篇文章主要介紹了mybatis查詢返回Map<String,Object>類型的講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
spring boot下mybatis配置雙數(shù)據(jù)源的實例
這篇文章主要介紹了spring boot下mybatis配置雙數(shù)據(jù)源的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09

