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

Java ThreadPoolExecutor的參數深入理解

 更新時間:2017年03月22日 15:06:57   作者:Lubby  
這篇文章主要介紹了Java ThreadPoolExecutor的參數深入理解的相關資料,需要的朋友可以參考下

Java ThreadPoolExecutor的參數深入理解

一、使用Executors創(chuàng)建線程池   

        之前創(chuàng)建線程的時候都是用的Executors的newFixedThreadPool(),newSingleThreadExecutor(),newCachedThreadPool()這三個方法。當然Executors也是用不同的參數去new ThreadPoolExecutor

    1. newFixedThreadPool()

    創(chuàng)建線程數固定大小的線程池。 由于使用了LinkedBlockingQueue所以maximumPoolSize 沒用,當corePoolSize滿了之后就加入到LinkedBlockingQueue隊列中。每當某個線程執(zhí)行完成之后就從LinkedBlockingQueue隊列中取一個。所以這個是創(chuàng)建固定大小的線程池。

 public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                   0L, TimeUnit.MILLISECONDS,
                   new LinkedBlockingQueue<Runnable>());
  }
 public ThreadPoolExecutor(int corePoolSize,
               int maximumPoolSize,
               long keepAliveTime,
               TimeUnit unit,
               BlockingQueue<Runnable> workQueue) {
   this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
       Executors.defaultThreadFactory(), defaultHandler);
  }

   2.newSingleThreadPool()

    創(chuàng)建線程數為1的線程池,由于使用了LinkedBlockingQueue所以maximumPoolSize 沒用,corePoolSize為1表示線程數大小為1,滿了就放入隊列中,執(zhí)行完了就從隊列取一個。

 public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
      (new ThreadPoolExecutor(1, 1,
                  0L, TimeUnit.MILLISECONDS,
                  new LinkedBlockingQueue<Runnable>()));
  }

    3.newCachedThreadPool()

    創(chuàng)建可緩沖的線程池。沒有大小限制。由于corePoolSize為0所以任務會放入SynchronousQueue隊列中,SynchronousQueue只能存放大小為1,所以會立刻新起線程,由于maxumumPoolSize為Integer.MAX_VALUE所以可以認為大小為2147483647。受內存大小限制。

public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                   60L, TimeUnit.SECONDS,
                   new SynchronousQueue<Runnable>());
  }
public ThreadPoolExecutor(int corePoolSize,
             int maximumPoolSize,
             long keepAliveTime,
             TimeUnit unit,
             BlockingQueue<Runnable> workQueue) {
    this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
       Executors.defaultThreadFactory(), defaultHandler);
  }
  

二、使用ThreadPoolExecutor創(chuàng)建線程池

ThreadPoolExecutor的構造函數

 public ThreadPoolExecutor(int corePoolSize,
               int maximumPoolSize,
               long keepAliveTime,
               TimeUnit unit,
               BlockingQueue<Runnable> workQueue,
               ThreadFactory threadFactory,
               RejectedExecutionHandler handler) {
    if (corePoolSize < 0 ||
      maximumPoolSize <= 0 ||
      maximumPoolSize < corePoolSize ||
      keepAliveTime < 0)
      throw new IllegalArgumentException();
    if (workQueue == null || threadFactory == null || handler == null)
      throw new NullPointerException();
    this.corePoolSize = corePoolSize;
    this.maximumPoolSize = maximumPoolSize;
    this.workQueue = workQueue;
    this.keepAliveTime = unit.toNanos(keepAliveTime);
    this.threadFactory = threadFactory;
    this.handler = handler;
  }

參數:

        1、corePoolSize核心線程數大小,當線程數<corePoolSize ,會創(chuàng)建線程執(zhí)行runnable

        2、maximumPoolSize 最大線程數, 當線程數 >= corePoolSize的時候,會把runnable放入workQueue中

        3、keepAliveTime  保持存活時間,當線程數大于corePoolSize的空閑線程能保持的最大時間。

        4、unit 時間單位

        5、workQueue 保存任務的阻塞隊列

        6、threadFactory 創(chuàng)建線程的工廠

        7、handler 拒絕策略

任務執(zhí)行順序:

        1、當線程數小于corePoolSize時,創(chuàng)建線程執(zhí)行任務。

        2、當線程數大于等于corePoolSize并且workQueue沒有滿時,放入workQueue中

        3、線程數大于等于corePoolSize并且當workQueue滿時,新任務新建線程運行,線程總數要小于maximumPoolSize

        4、當線程總數等于maximumPoolSize并且workQueue滿了的時候執(zhí)行handler的rejectedExecution。也就是拒絕策略。

ThreadPoolExecutor默認有四個拒絕策略:

        1、ThreadPoolExecutor.AbortPolicy()   直接拋出異常RejectedExecutionException

        2、ThreadPoolExecutor.CallerRunsPolicy()    直接調用run方法并且阻塞執(zhí)行

        3、ThreadPoolExecutor.DiscardPolicy()   直接丟棄后來的任務

        4、ThreadPoolExecutor.DiscardOldestPolicy()  丟棄在隊列中隊首的任務

當然可以自己繼承RejectedExecutionHandler來寫拒絕策略.

int corePoolSize = 1;
 int maximumPoolSize = 2;
 int keepAliveTime = 10;
// BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
 BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(5);
 ThreadFactory threadFactory = Executors.defaultThreadFactory();
 //線程池和隊列滿了之后的處理方式
 //1.跑出異常
 RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy(); 
 RejectedExecutionHandler handler2 = new ThreadPoolExecutor.CallerRunsPolicy();
 RejectedExecutionHandler handler3 = new ThreadPoolExecutor.DiscardPolicy();
 RejectedExecutionHandler handler4 = new ThreadPoolExecutor.DiscardOldestPolicy();

 
 ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue, threadFactory, handler2);
 
 
 for (int j = 1; j < 15; j++) {
  threadPoolExecutor.execute(new Runnable() {
  
  public void run() {
   
   try {
   System.out.println(Thread.currentThread().getName());
   TimeUnit.SECONDS.sleep(1);
   } catch (InterruptedException e) {
   e.printStackTrace();
   }
   
   
  }
  });
 }
 
 System.out.println(threadPoolExecutor);
 
 }


感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

  • Java基礎之垃圾回收機制詳解

    Java基礎之垃圾回收機制詳解

    這篇文章主要介紹了Java基礎之垃圾回收機制詳解,文中有非常詳細的代碼示例,對正在學習java基礎的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Java解壓zip文件完整代碼分享

    Java解壓zip文件完整代碼分享

    這篇文章主要介紹了Java解壓zip文件完整代碼分享,向大家分享了兩部分代碼示例,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • maven項目如何依賴自定jar包

    maven項目如何依賴自定jar包

    這篇文章主要介紹了maven項目如何依賴自定jar包,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • java 中http請求為了防止亂碼解決方案

    java 中http請求為了防止亂碼解決方案

    這篇文章主要介紹了java 中http請求為了防止亂碼解決方案的相關資料,需要的朋友可以參考下
    2017-02-02
  • 簡單理解Java的垃圾回收機制與finalize方法的作用

    簡單理解Java的垃圾回收機制與finalize方法的作用

    這篇文章主要介紹了簡單理解Java的垃圾回收機制與finalize方法的作用,著重講解了Java的GC銷毀對象的過程,需要的朋友可以參考下
    2015-11-11
  • MyBatis-Plus 修改和添加自動填充時間方式

    MyBatis-Plus 修改和添加自動填充時間方式

    這篇文章主要介紹了MyBatis-Plus 修改和添加自動填充時間方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringCloud集成Nacos的使用小結

    SpringCloud集成Nacos的使用小結

    這篇文章主要介紹了SpringCloud集成Nacos的使用小結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • spring boot基于Java的容器配置講解

    spring boot基于Java的容器配置講解

    這篇文章主要介紹了spring boot基于Java的容器配置講解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • 搭建JavaWeb服務器步驟詳解

    搭建JavaWeb服務器步驟詳解

    本篇文章主要給大家詳細分享了搭建JavaWeb服務器的詳細步驟以及用到的代碼,對此有需要的朋友可以跟著學習下。
    2018-02-02
  • SpringBoot集成WebSocket的兩種方式(JDK內置版和Spring封裝版)

    SpringBoot集成WebSocket的兩種方式(JDK內置版和Spring封裝版)

    這篇文章主要介紹了SpringBoot集成WebSocket的兩種方式,這兩種方式為JDK內置版和Spring封裝版,本文結合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2023-06-06

最新評論