Java 線程池框架
一、線程池結(jié)構(gòu)圖

二、示例
定義線程接口
public class MyThread extends Thread {
@Override
publicvoid run() {
System.out.println(Thread.currentThread().getName() + "正在執(zhí)行");
}
}
1:newSingleThreadExecutor
ExecutorService pool = Executors. newSingleThreadExecutor(); Thread t1 = new MyThread(); Thread t2 = new MyThread(); Thread t3 = new MyThread(); //將線程放入池中進(jìn)行執(zhí)行 pool.execute(t1); pool.execute(t2); pool.execute(t3); //關(guān)閉線程池 pool.shutdown();
輸入結(jié)果:
pool-1-thread-1正在執(zhí)行 pool-1-thread-1正在執(zhí)行 pool-1-thread-1正在執(zhí)行
2:newFixedThreadPool
ExecutorService pool = Executors.newFixedThreadPool(3); Thread t1 = new MyThread(); Thread t2 = new MyThread(); Thread t3 = new MyThread(); Thread t4 = new MyThread(); Thread t5 = new MyThread(); //將線程放入池中進(jìn)行執(zhí)行 pool.execute(t1); pool.execute(t2); pool.execute(t3); pool.execute(t4); pool.execute(t5); pool.shutdown();
輸入結(jié)果:
pool-1-thread-1正在執(zhí)行 pool-1-thread-2正在執(zhí)行 pool-1-thread-1正在執(zhí)行 pool-1-thread-2正在執(zhí)行
3 :newCachedThreadPool
ExecutorService pool = Executors.newCachedThreadPool(); Thread t1 = new MyThread(); Thread t2 = new MyThread(); Thread t3 = new MyThread(); Thread t4 = new MyThread(); Thread t5 = new MyThread(); //將線程放入池中進(jìn)行執(zhí)行 pool.execute(t1); pool.execute(t2); pool.execute(t3); pool.execute(t4); pool.execute(t5); //關(guān)閉線程池 pool.shutdown();
輸入結(jié)果:
pool-1-thread-2正在執(zhí)行 pool-1-thread-4正在執(zhí)行 pool-1-thread-3正在執(zhí)行 pool-1-thread-1正在執(zhí)行 pool-1-thread-5正在執(zhí)行
4 :ScheduledThreadPoolExecutor
ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
pool.scheduleAtFixedRate(new Runnable() {//每隔一段時間就觸發(fā)異常
@Override
public void run() {
//throw new RuntimeException();
System.out.println("================");
}
}, 1000, 2000, TimeUnit.MILLISECONDS);
pool.scheduleAtFixedRate(new Runnable() {//每隔一段時間打印系統(tǒng)時間,證明兩者是互不影響的
@Override
public void run() {
System.out.println("+++++++++++++++++");
}
}, 1000, 2000, TimeUnit.MILLISECONDS);
輸入結(jié)果:
================ +++++++++++++++++ +++++++++++++++++ +++++++++++++++++
三、線程池核心參數(shù)
corePoolSize : 池中核心的線程數(shù)
maximumPoolSize : 池中允許的最大線程數(shù)。
keepAliveTime : 當(dāng)線程數(shù)大于核心時,此為終止前多余的空閑線程等待新任務(wù)的最長時間。
unit : keepAliveTime 參數(shù)的時間單位。
workQueue : 執(zhí)行前用于保持任務(wù)的隊列。此隊列僅保持由 execute方法提交的 Runnable任務(wù)。
threadFactory : 執(zhí)行程序創(chuàng)建新線程時使用的工廠。
handler : 由于超出線程范圍和隊列容量而使執(zhí)行被阻塞時所使用的處理程序。
ThreadPoolExecutor :Executors類的底層實(shí)現(xiàn)。
3.1 任務(wù)排隊機(jī)制
SynchonousQueue: 同步隊列,隊列直接提交給線程執(zhí)行而不保持它們,此時線程池通常是無界的
LinkedBlockingQueue: 無界對列,當(dāng)線程池線程數(shù)達(dá)到最大數(shù)量時,新任務(wù)就會在隊列中等待執(zhí)行,可能會造成隊列無限膨脹
ArrayBlockingQueue : 有界隊列,有助于防止資源耗盡,一旦達(dá)到上限,可能會造成新任務(wù)丟失
注意:
newSingleThreadExecutor、newFixedThreadPool使用的是LinkedBlockingQueue
newCachedThreadPool 使用的是 SynchonousQueue
newScheduledThreadPool使用的是 DelayedWorkQueue
3.2 線程執(zhí)行流程

3.3 線程大小確定:
cpu密集型: 盡量少開線程,最佳線程數(shù) Ncpu+1
io密集型:多開線程,2Ncpu
混合型:根據(jù)情況而定,可以拆分成io密集和cou密集
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
Java畢業(yè)設(shè)計實(shí)戰(zhàn)之共享租車信息管理系統(tǒng)的實(shí)現(xiàn)
這是一個使用了java+Jsp+Servlet+Jdbc+Mysql開發(fā)的共享租車信息管理系統(tǒng),是一個畢業(yè)設(shè)計的實(shí)戰(zhàn)練習(xí),具有租車管理該有的所有功能,感興趣的朋友快來看看吧2022-02-02
java使用Dijkstra算法實(shí)現(xiàn)單源最短路徑
這篇文章主要為大家詳細(xì)介紹了java使用Dijkstra算法實(shí)現(xiàn)單源最短路徑,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
spring+hibernate 兩種整合方式配置文件的方法
本篇文章主要介紹了spring+hibernate 兩種整合方式配置文件的方法,主要有兩種方式 1、注解方式 2、xml方式實(shí)現(xiàn),有興趣的可以了解一下。2017-04-04
springboot schedule 解決定時任務(wù)不執(zhí)行的問題
這篇文章主要介紹了springboot schedule 解決定時任務(wù)不執(zhí)行的問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
Spring MVC項目中l(wèi)og4J和AOP使用詳解
項目日志記錄是項目開發(fā)、運(yùn)營必不可少的內(nèi)容,有了它可以對系統(tǒng)有整體的把控,出現(xiàn)任何問題都有蹤跡可尋。下面這篇文章主要給大家介紹了關(guān)于Spring MVC項目中l(wèi)og4J和AOP使用的相關(guān)資料,需要的朋友可以參考下。2017-12-12

