- 相關推薦
java線程池框架解析
使用Java中成型的框架來幫助我們開發并發應用即可以節省構建項目的時間,也可以提高應用的性能。以下是小編為大家搜索整理java線程池框架解析,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!
Java對象實例的鎖一共有四種狀態:無鎖,偏向鎖,輕量鎖和重量鎖。原始脫離框架的并發應用大部分都需要手動完成加鎖釋放,最直接的就是使用synchronized和volatile關鍵字對某個對象或者代碼塊加鎖從而限制每次訪問的次數,從對象之間的競爭也可以實現到對象之間的協作。但是這樣手動實現出來的應用不僅耗費時間而且性能表現往往又有待提升。
一、線程池結構圖
二、示例
定義線程接口
6public class MyThread extends Thread {@Overridepublicvoid run() {System.out.println(Thread.currentThread().getName() + "正在執行");}}
1:newSingleThreadExecutor
10ExecutorService pool = Executors. newSingleThreadExecutor();Thread t1 = new MyThread();Thread t2 = new MyThread();Thread t3 = new MyThread();//將線程放入池中進行執行pool.execute(t1);pool.execute(t2);pool.execute(t3);//關閉線程池pool.shutdown();
輸入結果:
3pool-1-thread-1正在執行pool-1-thread-1正在執行pool-1-thread-1正在執行
2:newFixedThreadPool
13ExecutorService pool = Executors.newFixedThreadPool(3);Thread t1 = new MyThread();Thread t2 = new MyThread();Thread t3 = new MyThread();Thread t4 = new MyThread();Thread t5 = new MyThread();//將線程放入池中進行執行pool.execute(t1);pool.execute(t2);pool.execute(t3);pool.execute(t4);pool.execute(t5);pool.shutdown();
輸入結果:
4pool-1-thread-1正在執行pool-1-thread-2正在執行pool-1-thread-1正在執行pool-1-thread-2正在執行
3 :newCachedThreadPool
14ExecutorService pool = Executors.newCachedThreadPool();Thread t1 = new MyThread();Thread t2 = new MyThread();Thread t3 = new MyThread();Thread t4 = new MyThread();Thread t5 = new MyThread();//將線程放入池中進行執行pool.execute(t1);pool.execute(t2);pool.execute(t3);pool.execute(t4);pool.execute(t5);//關閉線程池pool.shutdown();
輸入結果:
5pool-1-thread-2正在執行pool-1-thread-4正在執行pool-1-thread-3正在執行pool-1-thread-1正在執行pool-1-thread-5正在執行
4 :ScheduledThreadPoolExecutor
14ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);pool.scheduleAtFixedRate(new Runnable() {//每隔一段時間就觸發異常 @Override public void run() { //throw new RuntimeException(); System.out.println("================"); }}, 1000, 2000, TimeUnit.MILLISECONDS);pool.scheduleAtFixedRate(new Runnable() {//每隔一段時間打印系統時間,證明兩者是互不影響的 @Override public void run() { System.out.println("+++++++++++++++++"); }}, 1000, 2000, TimeUnit.MILLISECONDS);
輸入結果:
4================+++++++++++++++++++++++++++++++++++++++++++++++++++
三、線程池核心參數
corePoolSize : 池中核心的線程數
maximumPoolSize : 池中允許的最大線程數。
keepAliveTime : 當線程數大于核心時,此為終止前多余的空閑線程等待新任務的最長時間。
unit : keepAliveTime 參數的時間單位。
workQueue : 執行前用于保持任務的隊列。此隊列僅保持由 execute方法提交的 Runnable任務。
threadFactory : 執行程序創建新線程時使用的工廠。
handler : 由于超出線程范圍和隊列容量而使執行被阻塞時所使用的處理程序。
ThreadPoolExecutor :Executors類的底層實現。
3.1 任務排隊機制
SynchonousQueue: 同步隊列,隊列直接提交給線程執行而不保持它們,此時線程池通常是無界的
LinkedBlockingQueue: 無界對列,當線程池線程數達到最大數量時,新任務就會在隊列中等待執行,可能會造成隊列無限膨脹
ArrayBlockingQueue : 有界隊列,有助于防止資源耗盡,一旦達到上限,可能會造成新任務丟失
注意:
newSingleThreadExecutor、newFixedThreadPool使用的是LinkedBlockingQueue
newCachedThreadPool 使用的是 SynchonousQueue
newScheduledThreadPool使用的是 DelayedWorkQueue
3.2 線程執行流程
3.3 線程大小確定:
cpu密集型: 盡量少開線程,最佳線程數 Ncpu+1
io密集型:多開線程,2Ncpu
混合型:根據情況而定,可以拆分成io密集和cou密集
【java線程池框架解析】相關文章:
JAVA多線程之線程間的通信方式解析07-14
java中通用的線程池實例代碼08-27
java多線程08-31
java的多線程09-09
java語言的多線程08-29
java線程的幾種狀態10-22
Java線程編程中的主線程詳細介紹09-05
關于Java多線程介紹09-09
java多線程-線程通信實例詳細解讀07-07
Java多線程的開發技巧10-16