- 線程也是一種資源,多線程場景下頻繁的創(chuàng)建和銷毀線程均會產(chǎn)生性能損耗,過多不合理線程的創(chuàng)建也會占用大量系統(tǒng)資源、甚至造成系統(tǒng)宕機(jī)。在這個(gè)背景下“線程池”應(yīng)運(yùn)而生
- 線程池創(chuàng)建通過Executors產(chǎn)生ExecutorServices實(shí)現(xiàn)
ExecutorService threadPool = Executor.new******; // 創(chuàng)建線程池,可以創(chuàng)建不同類型線程池
threadPool.submit(Runnable); // 線程池申請線程,執(zhí)行Runnable中的run方法
threadPool.shotDown();// 關(guān)閉線程池,釋放占用資源
- 可以創(chuàng)建固定大小的線程池newFixedThreadPool,當(dāng)申請線程超過最大線程數(shù)是,則進(jìn)入等待隊(duì)列(LinkedBlockingQueue,先進(jìn)先執(zhí)行),直到有任務(wù)執(zhí)行完成
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author Rambo(浩祥)
* @create 2017-03-09
**/
public class Practice {
static int j = 0;
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
for (int i=0; i<5; i++){
j = i;
executorService.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ":" + j);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
executorService.shutdown();
}
}

image.png
- 可以創(chuàng)建緩存線程池newCachedThreadPool,申請線程如果當(dāng)前線程池沒有空閑線程,則創(chuàng)建,任務(wù)執(zhí)行完畢會放入線程池維護(hù),如果有空閑線程,則直接使用。緩存的線程超過一定時(shí)間會被銷毀回收(注意事項(xiàng):啟動的線程數(shù)如果超過整型最大值后會拋出RejectedExecutionException異常,啟動后的線程存活時(shí)間為一分鐘)
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author Rambo(浩祥)
* @create 2017-03-09
**/
public class Practice {
static int j = 0;
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i=0; i<5; i++){
j = i;
executorService.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ":" + j);
}
});
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
executorService.shutdown();
}
}

image.png
- 創(chuàng)建一個(gè)可定時(shí)執(zhí)行的線程池(類似Timer.schedual)newScheduledThreadPool,該線程池內(nèi)的線程沒有執(zhí)行序列,隨機(jī)撈取空閑線程,比如2個(gè)線程但有可能一直用的是一個(gè)線程
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @author Rambo(浩祥)
* @create 2017-03-09
**/
public class Practice {
static int j = 0;
public static void main(String[] args) {
ExecutorService executorService = Executors.newScheduledThreadPool(2);
ScheduledExecutorService executorService1 = Executors.newScheduledThreadPool(2);
for (int i=0; i<5; i++){
j = i;
executorService.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ":" + j);
}
});
}
executorService1.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "scheduleWithFixedDelay");
}
}, 1, 1, TimeUnit.SECONDS);
executorService1.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "scheduleAtFixedRate");
}
}, 1, 1, TimeUnit.SECONDS);
executorService.shutdown();
}
}

image.png