解讀線程池-Executors的newSingleThreadExecutor和newFixedThreadPool(1)區(qū)別
更新時間:2024年08月06日 14:46:35 作者:Ahuuua
這篇文章主要介紹了解讀線程池-Executors的newSingleThreadExecutor和newFixedThreadPool(1)區(qū)別,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
線程池Executors的newSingleThreadExecutor和newFixedThreadPool(1)
與其他等效的newFixedThreadPool(1)不同
- newSingleThreadExecutor返回的執(zhí)行器保證不可重新配置。
與其他等效的newScheduledThreadPool(1)不同
- newSingleThreadScheduledExecutor返回的執(zhí)行器保證不可重新配置以使用其他線程。
newFixedThreadPool(1)的返回結(jié)果我們可以通過強轉(zhuǎn)變成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("結(jié)束" + 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
* 開始
* 結(jié)束pool-1-thread-1
* 開始
* 結(jié)束pool-1-thread-1
* 開始
* 結(jié)束pool-1-thread-1
* 開始
* 結(jié)束pool-1-thread-1
* 開始
* 結(jié)束pool-1-thread-1
* 開始
* 結(jié)束pool-1-thread-1
* 開始
* 結(jié)束pool-1-thread-1
* 開始
* 結(jié)束pool-1-thread-1
* 開始
* 結(jié)束pool-1-thread-1
* 開始
* 結(jié)束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
* 開始
* 結(jié)束pool-2-thread-1
* 開始
* 結(jié)束pool-2-thread-1
* 開始
* 結(jié)束pool-2-thread-1
* 開始
* 結(jié)束pool-2-thread-1
* 開始
* 結(jié)束pool-2-thread-1
* 開始
* 結(jié)束pool-2-thread-1
* 開始
* 結(jié)束pool-2-thread-1
* 開始
* 結(jié)束pool-2-thread-1
* 開始
* 結(jié)束pool-2-thread-1
* 開始
* 結(jié)束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)
* 開始
* 開始
* 開始
* 開始
* 開始
* 結(jié)束pool-1-thread-1
* 結(jié)束pool-1-thread-5
* 結(jié)束pool-1-thread-2
* 結(jié)束pool-1-thread-3
* 結(jié)束pool-1-thread-4
* 開始
* 結(jié)束pool-1-thread-4
* 開始
* 結(jié)束pool-1-thread-4
* 開始
* 結(jié)束pool-1-thread-4
* 開始
* 結(jié)束pool-1-thread-4
* 開始
* 結(jié)束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)
*/
}
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java動態(tài)字節(jié)碼注入技術(shù)的實現(xiàn)
Java動態(tài)字節(jié)碼注入技術(shù)是一種在運行時修改Java字節(jié)碼的技術(shù),本文主要介紹了Java動態(tài)字節(jié)碼注入技術(shù)的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-08-08

