生活随笔
收集整理的這篇文章主要介紹了
Java四种线程池
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本文摘錄自:http://cuisuqiang.iteye.com/blog/2019372
?
Java通過Executors提供四種線程池,分別為:
newCachedThreadPool創(chuàng)建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。
newFixedThreadPool 創(chuàng)建一個定長線程池,可控制線程最大并發(fā)數(shù),超出的線程會在隊(duì)列中等待。
newScheduledThreadPool 創(chuàng)建一個定長線程池,支持定時及周期性任務(wù)執(zhí)行。
newSingleThreadExecutor 創(chuàng)建一個單線程化的線程池,它只會用唯一的工作線程來執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級)執(zhí)行。
?
(1) newCachedThreadPool
創(chuàng)建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。示例代碼如下:
1 package test;
2 import java.util.concurrent.ExecutorService;
3 import java.util.concurrent.Executors;
4 public class ThreadPoolExecutorTest {
5 public static void main(String[] args) {
6 ExecutorService cachedThreadPool =
Executors.newCachedThreadPool();
7 for (
int i = 0; i < 10; i++
) {
8 final int index =
i;
9 try {
10 Thread.sleep(index * 1000
);
11 }
catch (InterruptedException e) {
12 e.printStackTrace();
13 }
14 cachedThreadPool.execute(
new Runnable() {
15 public void run() {
16 System.out.println(index);
17 }
18 });
19 }
20 }
21 }
線程池為無限大,當(dāng)執(zhí)行第二個任務(wù)時第一個任務(wù)已經(jīng)完成,會復(fù)用執(zhí)行第一個任務(wù)的線程,而不用每次新建線程。
(2) newFixedThreadPool
創(chuàng)建一個定長線程池,可控制線程最大并發(fā)數(shù),超出的線程會在隊(duì)列中等待。示例代碼如下:
1 package test;
2 import java.util.concurrent.ExecutorService;
3 import java.util.concurrent.Executors;
4 public class ThreadPoolExecutorTest {
5 public static void main(String[] args) {
6 ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3
);
7 for (
int i = 0; i < 10; i++
) {
8 final int index =
i;
9 fixedThreadPool.execute(
new Runnable() {
10 public void run() {
11 try {
12 System.out.println(index);
13 Thread.sleep(2000
);
14 }
catch (InterruptedException e) {
15 e.printStackTrace();
16 }
17 }
18 });
19 }
20 }
21 }
因?yàn)榫€程池大小為3,每個任務(wù)輸出index后sleep 2秒,所以每兩秒打印3個數(shù)字。
定長線程池的大小最好根據(jù)系統(tǒng)資源進(jìn)行設(shè)置。如Runtime.getRuntime().availableProcessors()
?
(3)? newScheduledThreadPool
創(chuàng)建一個定長線程池,支持定時及周期性任務(wù)執(zhí)行。延遲執(zhí)行示例代碼如下:
1 package test;
2 import java.util.concurrent.Executors;
3 import java.util.concurrent.ScheduledExecutorService;
4 import java.util.concurrent.TimeUnit;
5 public class ThreadPoolExecutorTest {
6 public static void main(String[] args) {
7 ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5
);
8 scheduledThreadPool.schedule(
new Runnable() {
9 public void run() {
10 System.out.println("delay 3 seconds"
);
11 }
12 }, 3
, TimeUnit.SECONDS);
13 }
14 }
表示延遲3秒執(zhí)行。
定期執(zhí)行示例代碼如下:
1 package test;
2 import java.util.concurrent.Executors;
3 import java.util.concurrent.ScheduledExecutorService;
4 import java.util.concurrent.TimeUnit;
5 public class ThreadPoolExecutorTest {
6 public static void main(String[] args) {
7 ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5
);
8 scheduledThreadPool.scheduleAtFixedRate(
new Runnable() {
9 public void run() {
10 System.out.println("delay 1 seconds, and excute every 3 seconds"
);
11 }
12 }, 1, 3
, TimeUnit.SECONDS);
13 }
14 }
表示延遲1秒后每3秒執(zhí)行一次。
?
(4) newSingleThreadExecutor
創(chuàng)建一個單線程化的線程池,它只會用唯一的工作線程來執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級)執(zhí)行。示例代碼如下:
1 package test;
2 import java.util.concurrent.ExecutorService;
3 import java.util.concurrent.Executors;
4 public class ThreadPoolExecutorTest {
5 public static void main(String[] args) {
6 ExecutorService singleThreadExecutor =
Executors.newSingleThreadExecutor();
7 for (
int i = 0; i < 10; i++
) {
8 final int index =
i;
9 singleThreadExecutor.execute(
new Runnable() {
10 public void run() {
11 try {
12 System.out.println(index);
13 Thread.sleep(2000
);
14 }
catch (InterruptedException e) {
15 e.printStackTrace();
16 }
17 }
18 });
19 }
20 }
21 }
結(jié)果依次輸出,相當(dāng)于順序執(zhí)行各個任務(wù)。
轉(zhuǎn)載于:https://www.cnblogs.com/mayi1/p/5532121.html
總結(jié)
以上是生活随笔為你收集整理的Java四种线程池的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。