日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

java 線程池keepAliveTime的含義說明

瀏覽:118日期:2022-08-16 18:09:05

之前對線程池中屬性:keepAliveTime比較模糊,而且看過之后過一段時間就會忘掉,于是就在此記錄一下。

keepAliveTime的jdk中的解釋為:

當線程數大于核心時,此為終止前多余的空閑線程等待新任務的最長時間。

說的讓人感覺比較模糊,總結一下大概意思為:比如說線程池中最大的線程數為50,而其中只有40個線程任務在跑,相當于有10個空閑線程,這10個空閑線程不能讓他一直在開著,因為線程的存在也會特別好資源的,所有就需要設置一個這個空閑線程的存活時間,這么解釋應該就很清楚了。

這樣以后忘記了就過來看看就OK了。

補充:線程池的狀態及KeepAliveTime參數

五個狀態

// runState is stored in the high-order bits private static final int RUNNING = -1 << COUNT_BITS; private static final int SHUTDOWN = 0 << COUNT_BITS; private static final int STOP = 1 << COUNT_BITS; private static final int TIDYING = 2 << COUNT_BITS; private static final int TERMINATED = 3 << COUNT_BITS;循環getTask方法

/** * Performs blocking or timed wait for a task, depending on * current configuration settings, or returns null if this worker * must exit because of any of: * 1. There are more than maximumPoolSize workers (due to * a call to setMaximumPoolSize). * 2. The pool is stopped. * 3. The pool is shutdown and the queue is empty. * 4. This worker timed out waiting for a task, and timed-out * workers are subject to termination (that is, * {@code allowCoreThreadTimeOut || workerCount > corePoolSize}) * both before and after the timed wait. * * @return task, or null if the worker must exit, in which case * workerCount is decremented */ private Runnable getTask() { boolean timedOut = false; // Did the last poll() time out? retry: for (;;) { int c = ctl.get(); int rs = runStateOf(c); // Check if queue empty only if necessary. if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) { decrementWorkerCount(); return null; } boolean timed; // Are workers subject to culling? for (;;) { int wc = workerCountOf(c); timed = allowCoreThreadTimeOut || wc > corePoolSize; //默認allowCoreThreadTimeOut為false,除非程序指定 //(1)當沒有超過核心線程時,默認allowCoreThreadTimeOut為false時 //timed值為false,始終break掉,不會銷毀線程 //(2)當超過核心線程數,默認allowCoreThreadTimeOut為false時 //timed值為true,如果超過最大值,則銷毀;如果timeout過,則銷毀 // 如果allowCoreThreadTimeOut為true,則timed始終為true if (wc <= maximumPoolSize && ! (timedOut && timed)) break; if (compareAndDecrementWorkerCount(c)) return null; c = ctl.get(); // Re-read ctl if (runStateOf(c) != rs) continue retry; // else CAS failed due to workerCount change; retry inner loop } try { Runnable r = timed ? workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) : workQueue.take(); if (r != null) return r; timedOut = true; } catch (InterruptedException retry) { timedOut = false; } } }線程池狀態大于SHUTDOWN值的兩種情況1、調用shutdown方法

當線程池調用了shutdown方法,線程池的狀態會首先被設置為SHUTDOWN,然后遍歷線程池中所有線程,調用一次interrupt方法,如果在休眠中的線程將會激活,激活后的線程以及調用shutdown方法本身的線程都會嘗試去調用tryTerminate方法,該方法將判定如果線程池中所有記錄的線程數為0,則將線程狀態改為TERMINATED,這個值為3,將大于SHUTDOWN狀態值。

2、調用shutdownNow方法

當線程調用了shutdownNow方法后,首先將線程的狀態修改為STOP,這個狀態是大于SHUTDOWN值的,接下來它也會通過中斷激活線程,只是它來的更暴力一些,連加鎖和一些基本判斷都沒有,直接中斷;在調用tryTerminate之前會先清空阻塞隊列中所有的元素,這些元素被組裝為一個List列表作為shutdownNow方法的返回值。換句話說,沒有執行的任務在shutdownNow執行后的返回值中可以得到。在程序某些必要的情況下,可以通過線程池的isTerminating,isTerminated,isStopped,isShutdown來對線程做一些狀態判定。

KeepAliveTime參數

workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS)

當阻塞隊列中沒有任務時,等待時間達到keepAliveTime毫秒值時就會被自動喚醒,而不會永遠地沉睡下去。

keepAliveTime,如果是通過newCachedThreadPool的話,默認是1分鐘超時,如果遇到前面所提到的瞬間沖擊,那么線程池數量將瞬間快速膨脹,而且這些瞬間膨脹的線程的生命周期最少在1分鐘以上。

如果設置了該參數,那么當timeout的時候,就return null,就會跳出循環,回收線程。

if (wc <= maximumPoolSize && ! (timedOut && timed)) break; if (compareAndDecrementWorkerCount(c)) return null;

allowCoreThreadTimeout : 默認情況下核心線程不會退出,可通過將該參數設置為true,讓核心線程也退出。

默認的Executors工廠,只有newCachedThreadPool,timeout為60秒,出現timeout情況下,而且線程數超過了核心線程數,會銷毀銷毀線程。保持在corePoolSize數(如果是cached的,corePoolSize為0)。

/** * Timeout in nanoseconds for idle threads waiting for work. * Threads use this timeout when there are more than corePoolSize * present or if allowCoreThreadTimeOut. Otherwise they wait * forever for new work. */ private volatile long keepAliveTime; /** * If false (default), core threads stay alive even when idle. * If true, core threads use keepAliveTime to time out waiting * for work. */ private volatile boolean allowCoreThreadTimeOut;

線程池最小是corePoolSize,最大是maximumPoolSize,除非設置了allowCoreThreadTimeOut和超時時間,這種情況線程數可能減少到0,最大可能是Integer.MAX_VALUE。

Core pool size is the minimum number of workers to keep alive(and not allow to time out etc) unless allowCoreThreadTimeOut is set, in which case the minimum is zero.

/** * Creates a thread pool that creates new threads as needed, but * will reuse previously constructed threads when they are * available. These pools will typically improve the performance * of programs that execute many short-lived asynchronous tasks. * Calls to <tt>execute</tt> will reuse previously constructed * threads if available. If no existing thread is available, a new * thread will be created and added to the pool. Threads that have * not been used for sixty seconds are terminated and removed from * the cache. Thus, a pool that remains idle for long enough will * not consume any resources. Note that pools with similar * properties but different details (for example, timeout parameters) * may be created using {@link ThreadPoolExecutor} constructors. * * @return the newly created thread pool */ public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); } /** * Creates a thread pool that creates new threads as needed, but * will reuse previously constructed threads when they are * available, and uses the provided * ThreadFactory to create new threads when needed. * @param threadFactory the factory to use when creating new threads * @return the newly created thread pool * @throws NullPointerException if threadFactory is null */ public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory); }

超時timeout設置為0的話,表示不等待

public E poll(long timeout, TimeUnit unit) throws InterruptedException { return pollFirst(timeout, unit); }具體如下

public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { E x; while ( (x = unlinkFirst()) == null) { if (nanos <= 0) return null; nanos = notEmpty.awaitNanos(nanos); } return x; } finally { lock.unlock(); } }

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Java
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲欧美网站| 国产在线视频欧美一区| 播放一区二区| 国产福利电影在线播放| 国产精品欧美大片| 欧美日一区二区三区在线观看国产免| 日韩欧美另类中文字幕| 日本久久二区| 国产精品一区三区在线观看| 国产精品一区二区三区www| 国产精品1区| 国产成人精品一区二区三区免费| 精品三级av| 伊人久久在线| 国产综合亚洲精品一区二| 伊人影院久久| 亚洲精品护士| 国产精品一区高清| 亚洲国产福利| 日韩中文影院| 快she精品国产999| 欧美综合精品| 97精品一区| 不卡一区2区| 亚洲开心激情| 免费在线观看一区| 久久免费国产| 亚洲色图国产| 久久免费精品| 久久国产亚洲| 最新国产拍偷乱拍精品| 日韩中文av| 精品久久福利| 亚洲精品成人| 中文字幕成人| 麻豆91精品视频| 91精品国产自产在线观看永久∴| 亚洲一区二区日韩| 欧美激情 亚洲a∨综合| 快播电影网址老女人久久| 亚洲综合精品| 国产精品日本一区二区不卡视频 | 美女网站久久| 国产一卡不卡| 99精品视频精品精品视频| 日韩在线一二三区| 国产精品地址| 欧美日韩尤物久久| 中文字幕av一区二区三区人| 国产一区调教| 美女网站久久| 精品一区91| 亚洲一区激情| 国产一区二区三区国产精品| 日韩午夜av在线| 欧美黑人做爰爽爽爽| 最新亚洲一区| 欧美国产不卡| 99国产精品久久久久久久成人热| 国产欧美大片| 99在线|亚洲一区二区| 精品国产一区二区三区av片| 欧美 日韩 国产精品免费观看| 天堂久久av| 欧美不卡高清| 久久一区视频| 免费在线看一区| 国产精品精品| 日韩专区视频网站| 国产综合色产| 成人在线黄色| 婷婷亚洲成人| 久久麻豆精品| 欧美激情aⅴ一区二区三区| 久久国产精品99国产| а√在线中文在线新版| 日韩一区二区三区精品视频第3页| 国产一区二区三区91| 麻豆精品91| 欧美亚洲激情| 黄色欧美在线| 7777精品| 日韩制服丝袜av| 久久精品欧美一区| 久久中文在线| 911亚洲精品| 亚洲专区在线| 久久视频一区| 福利一区和二区| 国产亚洲一区二区三区不卡| 免费精品视频| 免费精品国产的网站免费观看| 国产精品www.| 色婷婷成人网| 免费精品视频在线| 欧美一区三区| 国产日韩电影| 国产一区二区三区探花| 久久国产精品色av免费看| 久久午夜精品| 欧美手机在线| 成人福利av| 黄色网一区二区| 免费视频一区二区三区在线观看| 日韩av网站在线观看| 蜜臀av性久久久久蜜臀aⅴ四虎| 久久久久久美女精品| 精品一区二区三区免费看| 久久国际精品| 日韩高清欧美激情| 视频国产精品| 亚洲精品在线国产| 日本不卡高清| 日韩免费精品| 最新中文字幕在线播放| 中文字幕高清在线播放| 97国产成人高清在线观看| 日韩综合在线| 日韩av在线中文字幕| 福利精品一区| 精品三级在线观看视频| 精品三级在线观看视频| 久久不见久久见免费视频7| 国产日韩欧美一区在线| 欧美一区激情| 国产精品久一| 美女精品久久| 国产成人久久精品一区二区三区| 国产第一亚洲| 日韩在线二区| 精品日韩视频| 国产99精品| 亚洲欧洲另类| 亚洲天堂免费| 欧美日韩 国产精品| 国产精品一区三区在线观看| 久久av国产紧身裤| 麻豆国产91在线播放| 国产精品一区二区三区四区在线观看 | 丝袜诱惑一区二区| 日韩理论视频| 欧美日韩国产一区二区三区不卡| 自拍日韩欧美| 视频一区免费在线观看| 一区二区三区四区精品视频| 日韩精品成人在线观看| 国产区精品区| 日韩精品久久久久久久电影99爱| 精品欧美久久| 午夜亚洲一区| 亚洲一区二区日韩| 国产欧美日韩综合一区在线播放| 欧美激情亚洲| 成人免费网站www网站高清| 日韩视频一区| 99视频一区| 91精品国产一区二区在线观看| 欧美黄页在线免费观看| 麻豆视频在线观看免费网站黄 | 欧美aa在线视频| 理论片午夜视频在线观看| 精品资源在线| 久久一区二区三区喷水| 欧美另类综合| 亚洲欧洲国产精品一区| 麻豆国产欧美日韩综合精品二区| 国产精品久久观看| 免费欧美一区| 奇米狠狠一区二区三区| 国产一区日韩| 亚洲一区二区三区高清不卡| 国产精品一区二区美女视频免费看| 日韩理论视频| 亚洲欧美激情诱惑| 国产精品美女午夜爽爽| 久久精品国产大片免费观看| 亚洲精品女人| 手机在线电影一区| 日韩精品一卡二卡三卡四卡无卡| 日韩中文在线电影| 亚洲欧洲日韩精品在线| 另类专区亚洲| 日韩一区二区三区高清在线观看| 日韩大片免费观看| 亚洲精品无播放器在线播放| 国产一区二区三区不卡视频网站 | 不卡中文一二三区| 欧美日韩亚洲国产精品| av资源亚洲| 亚洲久久视频| 亚洲午夜天堂| 亚洲欧美日韩国产一区| 精品视频在线你懂得| 国产视频久久| 麻豆国产精品一区二区三区| 亚洲一区二区动漫| 成人三级高清视频在线看| 日韩激情中文字幕| 国产精品99一区二区| 国产乱人伦丫前精品视频|