<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Java線程池七個參數(shù)詳解

          共 17393字,需瀏覽 35分鐘

           ·

          2021-04-17 19:14

          點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號”

          優(yōu)質(zhì)文章,第一時間送達(dá)

          源碼簡介

          ThreadPoolExecutor是JDK中的線程池實(shí)現(xiàn),這個類實(shí)現(xiàn)了一個線程池需要的各個方法,它提供了任務(wù)提交、線程管理、監(jiān)控等方法。


          下面是ThreadPoolExecutor類的構(gòu)造方法源碼,其他創(chuàng)建線程池的方法最終都會導(dǎo)向這個構(gòu)造方法,共有7個參數(shù):corePoolSize、maximumPoolSize、keepAliveTime、unit、workQueue、threadFactory、handler。

              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.acc = System.getSecurityManager() == null ?
                          null :
                          AccessController.getContext();
                  this.corePoolSize = corePoolSize;
                  this.maximumPoolSize = maximumPoolSize;
                  this.workQueue = workQueue;
                  this.keepAliveTime = unit.toNanos(keepAliveTime);
                  this.threadFactory = threadFactory;
                  this.handler = handler;
              }

          這些參數(shù)都通過volatile修飾:

          public class ThreadPoolExecutor extends AbstractExecutorService {
              private final BlockingQueue<Runnable> workQueue;
              private volatile ThreadFactory threadFactory;
              private volatile RejectedExecutionHandler handler;
              private volatile long keepAliveTime;
              // 是否允許核心線程被回收
              private volatile boolean allowCoreThreadTimeOut;
              private volatile int corePoolSize;
              private volatile int maximumPoolSize;
          }

          corePoolSize:核心線程數(shù)

          線程池維護(hù)的最小線程數(shù)量,核心線程創(chuàng)建后不會被回收(注意:設(shè)置allowCoreThreadTimeout=true后,空閑的核心線程超過存活時間也會被回收)。


          大于核心線程數(shù)的線程,在空閑時間超過keepAliveTime后會被回收。


          線程池剛創(chuàng)建時,里面沒有一個線程,當(dāng)調(diào)用 execute() 方法添加一個任務(wù)時,如果正在運(yùn)行的線程數(shù)量小于corePoolSize,則馬上創(chuàng)建新線程并運(yùn)行這個任務(wù)。


          maximumPoolSize:最大線程數(shù)

          線程池允許創(chuàng)建的最大線程數(shù)量。


          當(dāng)添加一個任務(wù)時,核心線程數(shù)已滿,線程池還沒達(dá)到最大線程數(shù),并且沒有空閑線程,工作隊(duì)列已滿的情況下,創(chuàng)建一個新線程,然后從工作隊(duì)列的頭部取出一個任務(wù)交由新線程來處理,而將剛提交的任務(wù)放入工作隊(duì)列尾部。


          keepAliveTime:空閑線程存活時間

          當(dāng)一個可被回收的線程的空閑時間大于keepAliveTime,就會被回收。

          可被回收的線程:

          (1)設(shè)置allowCoreThreadTimeout=true的核心線程。

          (2)大于核心線程數(shù)的線程(非核心線程)。

          unit:時間單位

          keepAliveTime的時間單位:

          TimeUnit.NANOSECONDS
          TimeUnit.MICROSECONDS
          TimeUnit.MILLISECONDS // 毫秒
          TimeUnit.SECONDS
          TimeUnit.MINUTES
          TimeUnit.HOURS
          TimeUnit.DAYS

          workQueue:工作隊(duì)列

          新任務(wù)被提交后,會先添加到工作隊(duì)列,任務(wù)調(diào)度時再從隊(duì)列中取出任務(wù)。工作隊(duì)列實(shí)現(xiàn)了BlockingQueue接口。


          JDK默認(rèn)的工作隊(duì)列有五種:

          (1)ArrayBlockingQueue 數(shù)組型阻塞隊(duì)列:數(shù)組結(jié)構(gòu),初始化時傳入大小,有界,F(xiàn)IFO,使用一個重入鎖,默認(rèn)使用非公平鎖,入隊(duì)和出隊(duì)共用一個鎖,互斥。

          (2)LinkedBlockingQueue 鏈表型阻塞隊(duì)列:鏈表結(jié)構(gòu),默認(rèn)初始化大小為Integer.MAX_VALUE,有界(近似無解),F(xiàn)IFO,使用兩個重入鎖分別控制元素的入隊(duì)和出隊(duì),用Condition進(jìn)行線程間的喚醒和等待。

          (3)SynchronousQueue 同步隊(duì)列:容量為0,添加任務(wù)必須等待取出任務(wù),這個隊(duì)列相當(dāng)于通道,不存儲元素。

          (4)PriorityBlockingQueue 優(yōu)先阻塞隊(duì)列:無界,默認(rèn)采用元素自然順序升序排列。

          (5)DelayQueue 延時隊(duì)列:無界,元素有過期時間,過期的元素才能被取出。


          threadFactory:線程工廠

          創(chuàng)建線程的工廠,可以設(shè)定線程名、線程編號等。

          默認(rèn)線程工廠:

              /**
               * The default thread factory
               */
              static class DefaultThreadFactory implements ThreadFactory {
                  private static final AtomicInteger poolNumber = new AtomicInteger(1);
                  private final ThreadGroup group;
                  private final AtomicInteger threadNumber = new AtomicInteger(1);
                  private final String namePrefix;
           
                  DefaultThreadFactory() {
                      SecurityManager s = System.getSecurityManager();
                      group = (s != null) ? s.getThreadGroup() :
                                            Thread.currentThread().getThreadGroup();
                      namePrefix = "pool-" +
                                    poolNumber.getAndIncrement() +
                                   "-thread-";
                  }
           
                  public Thread newThread(Runnable r) {
                      Thread t = new Thread(group, r,
                                            namePrefix + threadNumber.getAndIncrement(),
                                            0);
                      if (t.isDaemon())
                          t.setDaemon(false);
                      if (t.getPriority() != Thread.NORM_PRIORITY)
                          t.setPriority(Thread.NORM_PRIORITY);
                      return t;
                  }
              }


          handler:拒絕策略

          當(dāng)線程池線程數(shù)已滿,并且工作隊(duì)列達(dá)到限制,新提交的任務(wù)使用拒絕策略處理。可以自定義拒絕策略,拒絕策略需要實(shí)現(xiàn)RejectedExecutionHandler接口。

          JDK默認(rèn)的拒絕策略有四種:

          (1)AbortPolicy:丟棄任務(wù)并拋出RejectedExecutionException異常。

          (2)DiscardPolicy:丟棄任務(wù),但是不拋出異常。可能導(dǎo)致無法發(fā)現(xiàn)系統(tǒng)的異常狀態(tài)。

          (3)DiscardOldestPolicy:丟棄隊(duì)列最前面的任務(wù),然后重新提交被拒絕的任務(wù)。

          (4)CallerRunsPolicy:由調(diào)用線程處理該任務(wù)。

          默認(rèn)拒絕策略:

              /**
               * The default rejected execution handler
               */
              private static final RejectedExecutionHandler defaultHandler = new AbortPolicy();
           
              public static class AbortPolicy implements RejectedExecutionHandler {
                  /**
                   * Creates an {@code AbortPolicy}.
                   */
                  public AbortPolicy() { }
           
                  /**
                   * Always throws RejectedExecutionException.
                   *
                   * @param r the runnable task requested to be executed
                   * @param e the executor attempting to execute this task
                   * @throws RejectedExecutionException always
                   */
                  public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
                      throw new RejectedExecutionException("Task " + r.toString() +
                                                           " rejected from " +
                                                           e.toString());
                  }
              }


          自定義線程池工具

          import java.util.concurrent.*;
          import java.util.concurrent.atomic.AtomicInteger;
           
          /**
           * 線程池工廠工具
           *
           * @author 向振華
           * @date 2021/04/11 10:24
           */
          public class ThreadPoolFactory {
           
              /**
               * 生成固定大小的線程池
               *
               * @param threadName 線程名稱
               * @return 線程池
               */
              public static ExecutorService createFixedThreadPool(String threadName) {
                  AtomicInteger threadNumber = new AtomicInteger(0);
                  return new ThreadPoolExecutor(
                          // 核心線程數(shù)
                          desiredThreadNum(),
                          // 最大線程數(shù)
                          desiredThreadNum() * 2,
                          // 空閑線程存活時間
                          60L,
                          // 空閑線程存活時間單位
                          TimeUnit.SECONDS,
                          // 工作隊(duì)列
                          new ArrayBlockingQueue<>(1024),
                          // 線程工廠
                          new ThreadFactory() {
                              @Override
                              public Thread newThread(Runnable r) {
                                  return new Thread(r, threadName + "-" + threadNumber.getAndIncrement());
                              }
                          },
                          // 拒絕策略
                          new RejectedExecutionHandler() {
                              @Override
                              public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                                  if (!executor.isShutdown()) {
                                      try {
                                          //嘗試阻塞式加入任務(wù)隊(duì)列
                                          executor.getQueue().put(r);
                                      } catch (Exception e) {
                                          //保持線程的中斷狀態(tài)
                                          Thread.currentThread().interrupt();
                                      }
                                  }
                              }
                          });
              }
           
              /**
               * 理想的線程數(shù),使用 2倍cpu核心數(shù)
               */
              public static int desiredThreadNum() {
                  return Runtime.getRuntime().availableProcessors() * 2;
              }
          }



          ————————————————

          版權(quán)聲明:本文為CSDN博主「一個抓手」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。

          原文鏈接:

          https://blog.csdn.net/Anenan/article/details/115603481






          粉絲福利:Java從入門到入土學(xué)習(xí)路線圖

          ??????

          ??長按上方微信二維碼 2 秒


          感謝點(diǎn)贊支持下哈 

          瀏覽 87
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  97熟妇| 麻豆亚洲AV成人无码一区精品 | 国产字幕| 国产高潮视频在线观看 | 精品久久乐 |