JDK1.8 創(chuàng)建線程池有哪幾種方式?
JDK1.8 創(chuàng)建線程池有哪幾種方式?
newFixedThreadPool
定長線程池,每當提交一個任務就創(chuàng)建一個線程,直到達到線程池的最大數(shù)量,這時線程數(shù)量不再變化,當線程發(fā)生錯誤結束時,線程池會補充一個新的線程
測試代碼:
public class TestThreadPool {
//定長線程池,每當提交一個任務就創(chuàng)建一個線程,直到達到線程池的最大數(shù)量,這時線程數(shù)量不再變化,當線程發(fā)生錯誤結束時,線程池會補充一個新的線程
static ExecutorService fixedExecutor = Executors.newFixedThreadPool(3);
public static void main(String[] args) {
testFixedExecutor();
}
//測試定長線程池,線程池的容量為3,提交6個任務,根據打印結果可以看出先執(zhí)行前3個任務,3個任務結束后再執(zhí)行后面的任務
private static void testFixedExecutor() {
for (int i = 0; i < 6; i++) {
final int index = i;
fixedExecutor.execute(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " index:" + index);
}
});
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("4秒后...");
fixedExecutor.shutdown();
}
}
打印結果:
pool-1-thread-1 index:0
pool-1-thread-2 index:1
pool-1-thread-3 index:2
4秒后...
pool-1-thread-3 index:5
pool-1-thread-1 index:3
pool-1-thread-2 index:4
newCachedThreadPool
可緩存的線程池,如果線程池的容量超過了任務數(shù),自動回收空閑線程,任務增加時可以自動添加新線程,線程池的容量不限制
測試代碼:
public class TestThreadPool {
//可緩存的線程池,如果線程池的容量超過了任務數(shù),自動回收空閑線程,任務增加時可以自動添加新線程,線程池的容量不限制
static ExecutorService cachedExecutor = Executors.newCachedThreadPool();
public static void main(String[] args) {
testCachedExecutor();
}
//測試可緩存線程池
private static void testCachedExecutor() {
for (int i = 0; i < 6; i++) {
final int index = i;
cachedExecutor.execute(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " index:" + index);
}
});
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("4秒后...");
cachedExecutor.shutdown();
}
}
打印結果:
pool-1-thread-1 index:0
pool-1-thread-6 index:5
pool-1-thread-5 index:4
pool-1-thread-4 index:3
pool-1-thread-3 index:2
pool-1-thread-2 index:1
4秒后...
newScheduledThreadPool 定長線程池,可執(zhí)行周期性的任務
測試代碼:
public class TestThreadPool {
//定長線程池,可執(zhí)行周期性的任務
static ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(3);
public static void main(String[] args) {
testScheduledExecutor();
}
//測試定長、可周期執(zhí)行的線程池
private static void testScheduledExecutor() {
for (int i = 0; i < 3; i++) {
final int index = i;
//scheduleWithFixedDelay 固定的延遲時間執(zhí)行任務;scheduleAtFixedRate 固定的頻率執(zhí)行任務
scheduledExecutor.scheduleWithFixedDelay(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + " index:" + index);
}
}, 0, 3, TimeUnit.SECONDS);
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("4秒后...");
scheduledExecutor.shutdown();
}
}
打印結果:
pool-1-thread-1 index:0
pool-1-thread-2 index:1
pool-1-thread-3 index:2
pool-1-thread-1 index:0
pool-1-thread-3 index:1
pool-1-thread-1 index:2
4秒后...
newSingleThreadExecutor
單線程的線程池,線程異常結束,會創(chuàng)建一個新的線程,能確保任務按提交順序執(zhí)行
測試代碼:
public class TestThreadPool {
//單線程的線程池,線程異常結束,會創(chuàng)建一個新的線程,能確保任務按提交順序執(zhí)行
static ExecutorService singleExecutor = Executors.newSingleThreadExecutor();
public static void main(String[] args) {
testSingleExecutor();
}
//測試單線程的線程池
private static void testSingleExecutor() {
for (int i = 0; i < 3; i++) {
final int index = i;
singleExecutor.execute(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " index:" + index);
}
});
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("4秒后...");
singleExecutor.shutdown();
}
}
打印結果:
pool-1-thread-1 index:0
4秒后...
pool-1-thread-1 index:1
pool-1-thread-1 index:2
newSingleThreadScheduledExecutor
單線程可執(zhí)行周期性任務的線程池
測試代碼:
public class TestThreadPool {
//單線程可執(zhí)行周期性任務的線程池
static ScheduledExecutorService singleScheduledExecutor = Executors.newSingleThreadScheduledExecutor();
public static void main(String[] args) {
testSingleScheduledExecutor();
}
//測試單線程可周期執(zhí)行的線程池
private static void testSingleScheduledExecutor() {
for (int i = 0; i < 3; i++) {
final int index = i;
//scheduleWithFixedDelay 固定的延遲時間執(zhí)行任務;scheduleAtFixedRate 固定的頻率執(zhí)行任務
singleScheduledExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName() + " index:" + index);
}
}, 0, 3, TimeUnit.SECONDS);
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("4秒后...");
singleScheduledExecutor.shutdown();
}
}
打印結果:
pool-1-thread-1 index:0
pool-1-thread-1 index:1
pool-1-thread-1 index:2
pool-1-thread-1 index:0
pool-1-thread-1 index:1
pool-1-thread-1 index:2
4秒后...
newWorkStealingPool
任務竊取線程池,不保證執(zhí)行順序,適合任務耗時差異較大。
線程池中有多個線程隊列,有的線程隊列中有大量的比較耗時的任務堆積,而有的線程隊列卻是空的,就存在有的線程處于饑餓狀態(tài),當一個線程處于饑餓狀態(tài)時,它就會去其它的線程隊列中竊取任務。解決饑餓導致的效率問題。
默認創(chuàng)建的并行 level 是 CPU 的核數(shù)。主線程結束,即使線程池有任務也會立即停止。
測試代碼:
public class TestThreadPool {
//任務竊取線程池
static ExecutorService workStealingExecutor = Executors.newWorkStealingPool();
public static void main(String[] args) {
testWorkStealingExecutor();
}
//測試任務竊取線程池
private static void testWorkStealingExecutor() {
for (int i = 0; i < 10; i++) {//本機 CPU 8核,這里創(chuàng)建10個任務進行測試
final int index = i;
workStealingExecutor.execute(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " index:" + index);
}
});
}
try {
Thread.sleep(4000);//這里主線程不休眠,不會有打印輸出
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("4秒后...");
// workStealingExecutor.shutdown();
}
}
打印結果如下,index:8,index:9并未打印出:
ForkJoinPool-1-worker-1 index:0
ForkJoinPool-1-worker-7 index:6
ForkJoinPool-1-worker-5 index:4
ForkJoinPool-1-worker-3 index:2
ForkJoinPool-1-worker-4 index:3
ForkJoinPool-1-worker-2 index:1
ForkJoinPool-1-worker-0 index:7
ForkJoinPool-1-worker-6 index:5
4秒后...
感謝閱讀,希望對你有所幫助 :)
來源:blog.csdn.net/meism5/article/details/90261021
END
最近給大家找了 通用權限系統(tǒng)
資源,怎么領取?
掃二維碼,加我微信,回復:通用權限系統(tǒng)
注意,不要亂回復 沒錯,不是機器人 記得一定要等待,等待才有好東西
評論
圖片
表情
