• 當前位置:首頁 > IT技術 > 編程語言 > 正文

    java 線程池參數
    2021-12-01 23:01:00

    創建線程池的構造函數如下:

    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;
        }
    ?
    corePoolSize? ? ? ? 核心線程數,線程池中最小線程數,即使線程空閑,也不會被銷毀;任務首先提交到核心線程中執行。(初始化時不創建線程,提交任務時創建線程)(如果設置allowCoreThreadTimeOut為true,核心線程也會被銷毀,使用較少)
    maximumPoolSize 線程池中允許創建的最大線程數。
    keepAliveTime 當線程數大于corePoolSize時,線程最多等待keepAliveTime時間后,將被銷毀。
    unit keepAliveTime參數的時間單位,一般為毫秒。
    workQueue 提交任務的緩存隊列。
    threadFactory 創建線程的工廠。
    handler 當corePoolSize已滿,緩存隊列已滿,maximumPoolSize已滿時,又發生任務提交時的處理器。
    ?
    提交任務時:
    如果線程數小于核心線程數(corePoolSize)(或者核心線程有空閑),則新創建線程(或者使用空閑線程)執行任務;
    如果線程數等于核心線程數,則將任務緩存到隊列(workQueue);
    如果緩存隊列已滿,則繼續創建線程執行任務,直到達到最大線程數;
    如果線程數達到最大線程數(maximumPoolSize),再提交任務,將使用handler來處理無法處理的任務。

    本文摘自 :https://www.cnblogs.com/

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码