ThreadPool — 線程池
- 重用存在的線程,減少對象、消亡的開銷
- 線程總數(shù)可控,提高資源的利用率
- 避免過多資源競爭,避免阻塞
- 提供額外功能,定時執(zhí)行,定期執(zhí)行,監(jiān)控等
線程池的種類
- 在java.util.concurrent中,提供了工具類Executors(調(diào)度器)對象來創(chuàng)建線程池,可創(chuàng)建的線程池有四種:
- CachedThreadPool — 可緩存線程池
- FixedThreadPool — 定長線程池
- SingleThreadExecutor — 單線程池
- ScheduledThreadPool — 調(diào)度線程池
1. CachedThreadPool 例子
/**
* 可緩存線程池
*/
public class CachedThreadPoolSample {
public static void main(String[] args) {
//調(diào)度器對象 ExecutorService用于管理線程池
//創(chuàng)建一個可緩存線程池, 可緩存線程池的特點是,無限大,如果線程池中沒有可用的線程則創(chuàng)建,有則使用空閑的線程
ExecutorService threadPool = Executors.newCachedThreadPool();
for (int i = 1;i<=1000;i++){
final int index = i;
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " : " + index);
}
});
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//關(guān)閉線程池
threadPool.shutdown();
}
}
運行結(jié)果
...
pool-1-thread-217 : 718
pool-1-thread-90 : 717
pool-1-thread-218 : 713
pool-1-thread-28 : 714
pool-1-thread-3 : 712
pool-1-thread-219 : 703
pool-1-thread-32 : 710
pool-1-thread-88 : 709
2. FixedThreadPool — 定長線程池
/**
* 定長線程池
*/
public class FixedThreadPoolSample {
public static void main(String[] args) {
//調(diào)度器對象 ExecutorService用于管理線程池
//創(chuàng)建一個定長線程池,固定線程總數(shù),空閑線程用于執(zhí)行任務(wù),如果線程都在使用后續(xù)任務(wù)則處于等待狀態(tài)
ExecutorService threadPool = Executors.newFixedThreadPool(3);
//如果任務(wù)處于等待的狀態(tài),備選的等待算法默認為FIFO(先進先出)。 LIFO(后進先出)
for (int i = 1;i<=1000;i++){
final int index = i;
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " : " + index);
}
});
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//關(guān)閉線程池
threadPool.shutdown();
}
}
運行結(jié)果
...
pool-1-thread-1 : 997
pool-1-thread-1 : 998
pool-1-thread-1 : 999
pool-1-thread-1 : 1000
pool-1-thread-2 : 740
pool-1-thread-3 : 972
3. SingleThreadExecutor — 單線程池
/**
* 單線程線程池
*/
public class SingleThreadPoolSample {
public static void main(String[] args) {
//調(diào)度器對象 ExecutorService用于管理線程池
//創(chuàng)建一個單線程線程池
ExecutorService threadPool = Executors.newSingleThreadExecutor();
for (int i = 1;i<=1000;i++){
final int index = i;
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " : " + index);
}
});
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//關(guān)閉線程池
threadPool.shutdown();
}
}
運行結(jié)果
pool-1-thread-1 : 994
pool-1-thread-1 : 995
pool-1-thread-1 : 996
pool-1-thread-1 : 997
pool-1-thread-1 : 998
pool-1-thread-1 : 999
pool-1-thread-1 : 1000
4. ScheduledThreadPool — 調(diào)度線程池
/**
* 調(diào)度線程池
*/
public class ScheduledThreadPoolSample {
public static void main(String[] args) {
//創(chuàng)建可調(diào)度線程池 初始化5個線程
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
//延遲三秒 只執(zhí)行一次run()方法
scheduledThreadPool.schedule(new Runnable() {
@Override
public void run() {
System.out.println("延遲3秒執(zhí)行");
}
}, 3, TimeUnit.SECONDS);
//延遲1秒執(zhí)行,每三秒執(zhí)行一次
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println(new Date() + "延遲1秒執(zhí)行,每三秒執(zhí)行一次");
}
}, 1, 3, TimeUnit.SECONDS);
/**
* 項目實際開發(fā)中ScheduledExecutorService 與 Timer都不會用到
* 因為有成熟的調(diào)度框架quartz , 或者spring自帶調(diào)度
* 程序的調(diào)度框架支持一種表達式 Cron表達式
*/
}
}
運行結(jié)果
Tue Aug 07 13:00:01 CST 2018延遲1秒執(zhí)行,沒三秒執(zhí)行一次
Tue Aug 07 13:00:04 CST 2018延遲1秒執(zhí)行,沒三秒執(zhí)行一次
Tue Aug 07 13:00:07 CST 2018延遲1秒執(zhí)行,沒三秒執(zhí)行一次
Tue Aug 07 13:00:10 CST 2018延遲1秒執(zhí)行,沒三秒執(zhí)行一次
Tue Aug 07 13:00:13 CST 2018延遲1秒執(zhí)行,沒三秒執(zhí)行一次
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。