欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 線程池框架

 更新時間:2017年02月08日 15:52:33   作者:月光下的鳳尾竹  
本文主要介紹了Java 線程池框架的相關(guān)知識。具有很好的參考價值,下面跟著小編一起來看下吧

一、線程池結(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類的底層實現(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使用JDBC連接數(shù)據(jù)庫的詳細(xì)步驟

    Java使用JDBC連接數(shù)據(jù)庫的詳細(xì)步驟

    本文詳細(xì)講解了Java使用JDBC連接數(shù)據(jù)庫的詳細(xì)步驟,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • 基于Java方式實現(xiàn)數(shù)據(jù)同步

    基于Java方式實現(xiàn)數(shù)據(jù)同步

    這篇文章主要為大家詳細(xì)介紹了基于Java方式實現(xiàn)數(shù)據(jù)同步,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 利用Java讀取Word表格中文本和圖片的方法實例

    利用Java讀取Word表格中文本和圖片的方法實例

    這篇文章主要給大家介紹了關(guān)于如何利用Java讀取Word表格中文本和圖片的相關(guān)資料,主要利用的是free spire.doc.jar 包,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2021-07-07
  • SpringBoot啟動java.nio.charset.MalformedInputException: Input length = 1報錯的解決方案

    SpringBoot啟動java.nio.charset.MalformedInputException: I

    本文主要介紹了SpringBoot啟動java.nio.charset.MalformedInputException: Input length = 1報錯的解決方案
    2023-07-07
  • 基于java高并發(fā)處理方案

    基于java高并發(fā)處理方案

    這篇文章主要介紹了基于java高并發(fā)處理方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • SpringBoot事務(wù)注解超詳細(xì)講解

    SpringBoot事務(wù)注解超詳細(xì)講解

    這篇文章主要給大家介紹了關(guān)于SpringBoot事務(wù)注解的相關(guān)資料,在Spring Boot中,事務(wù)管理是一個非常重要的概念,我們可以使用事務(wù)注解來控制事務(wù)的行為,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • springboot項目打docker鏡像實例(入門級)

    springboot項目打docker鏡像實例(入門級)

    最近做個項目,我們想把自己的程序打包成鏡像,并運行在docker容器中,本文主要介紹了springboot項目打docker鏡像實例,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • hibernate批量操作實例詳解

    hibernate批量操作實例詳解

    這篇文章主要介紹了hibernate批量操作,結(jié)合實例形式分析了Hibernate實現(xiàn)批量插入,更新及刪除等操作的具體實現(xiàn)技巧,需要的朋友可以參考下
    2016-03-03
  • java使用XSSFWorkbook實現(xiàn)讀寫Excel

    java使用XSSFWorkbook實現(xiàn)讀寫Excel

    這篇文章主要為大家詳細(xì)介紹了java如何通過使用XSSFWorkbook實現(xiàn)讀寫Excel功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-04-04
  • Java排序算法總結(jié)之選擇排序

    Java排序算法總結(jié)之選擇排序

    這篇文章主要介紹了Java排序算法總結(jié)之選擇排序,較為詳細(xì)的分析了選擇排序的原理與java實現(xiàn)技巧,需要的朋友可以參考下
    2015-05-05

最新評論