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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java 線程池keepAliveTime的含義說(shuō)明

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

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

keepAliveTime的jdk中的解釋為:

當(dāng)線程數(shù)大于核心時(shí),此為終止前多余的空閑線程等待新任務(wù)的最長(zhǎng)時(shí)間。

說(shuō)的讓人感覺(jué)比較模糊,總結(jié)一下大概意思為:比如說(shuō)線程池中最大的線程數(shù)為50,而其中只有40個(gè)線程任務(wù)在跑,相當(dāng)于有10個(gè)空閑線程,這10個(gè)空閑線程不能讓他一直在開(kāi)著,因?yàn)榫€程的存在也會(huì)特別好資源的,所有就需要設(shè)置一個(gè)這個(gè)空閑線程的存活時(shí)間,這么解釋?xiě)?yīng)該就很清楚了。

這樣以后忘記了就過(guò)來(lái)看看就OK了。

補(bǔ)充:線程池的狀態(tài)及KeepAliveTime參數(shù)

五個(gè)狀態(tài)

// 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;循環(huán)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; //默認(rèn)allowCoreThreadTimeOut為false,除非程序指定 //(1)當(dāng)沒(méi)有超過(guò)核心線程時(shí),默認(rèn)allowCoreThreadTimeOut為false時(shí) //timed值為false,始終break掉,不會(huì)銷毀線程 //(2)當(dāng)超過(guò)核心線程數(shù),默認(rèn)allowCoreThreadTimeOut為false時(shí) //timed值為true,如果超過(guò)最大值,則銷毀;如果timeout過(guò),則銷毀 // 如果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; } } }線程池狀態(tài)大于SHUTDOWN值的兩種情況1、調(diào)用shutdown方法

當(dāng)線程池調(diào)用了shutdown方法,線程池的狀態(tài)會(huì)首先被設(shè)置為SHUTDOWN,然后遍歷線程池中所有線程,調(diào)用一次interrupt方法,如果在休眠中的線程將會(huì)激活,激活后的線程以及調(diào)用shutdown方法本身的線程都會(huì)嘗試去調(diào)用tryTerminate方法,該方法將判定如果線程池中所有記錄的線程數(shù)為0,則將線程狀態(tài)改為T(mén)ERMINATED,這個(gè)值為3,將大于SHUTDOWN狀態(tài)值。

2、調(diào)用shutdownNow方法

當(dāng)線程調(diào)用了shutdownNow方法后,首先將線程的狀態(tài)修改為STOP,這個(gè)狀態(tài)是大于SHUTDOWN值的,接下來(lái)它也會(huì)通過(guò)中斷激活線程,只是它來(lái)的更暴力一些,連加鎖和一些基本判斷都沒(méi)有,直接中斷;在調(diào)用tryTerminate之前會(huì)先清空阻塞隊(duì)列中所有的元素,這些元素被組裝為一個(gè)List列表作為shutdownNow方法的返回值。換句話說(shuō),沒(méi)有執(zhí)行的任務(wù)在shutdownNow執(zhí)行后的返回值中可以得到。在程序某些必要的情況下,可以通過(guò)線程池的isTerminating,isTerminated,isStopped,isShutdown來(lái)對(duì)線程做一些狀態(tài)判定。

KeepAliveTime參數(shù)

workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS)

當(dāng)阻塞隊(duì)列中沒(méi)有任務(wù)時(shí),等待時(shí)間達(dá)到keepAliveTime毫秒值時(shí)就會(huì)被自動(dòng)喚醒,而不會(huì)永遠(yuǎn)地沉睡下去。

keepAliveTime,如果是通過(guò)newCachedThreadPool的話,默認(rèn)是1分鐘超時(shí),如果遇到前面所提到的瞬間沖擊,那么線程池?cái)?shù)量將瞬間快速膨脹,而且這些瞬間膨脹的線程的生命周期最少在1分鐘以上。

如果設(shè)置了該參數(shù),那么當(dāng)timeout的時(shí)候,就return null,就會(huì)跳出循環(huán),回收線程。

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

allowCoreThreadTimeout : 默認(rèn)情況下核心線程不會(huì)退出,可通過(guò)將該參數(shù)設(shè)置為true,讓核心線程也退出。

默認(rèn)的Executors工廠,只有newCachedThreadPool,timeout為60秒,出現(xiàn)timeout情況下,而且線程數(shù)超過(guò)了核心線程數(shù),會(huì)銷毀銷毀線程。保持在corePoolSize數(shù)(如果是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,除非設(shè)置了allowCoreThreadTimeOut和超時(shí)時(shí)間,這種情況線程數(shù)可能減少到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); }

超時(shí)timeout設(shè)置為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(); } }

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Java
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
四虎4545www国产精品| 国产福利一区二区精品秒拍 | 亚洲理论在线| 亚洲国产不卡| 99国内精品| 蜜臀av性久久久久蜜臀aⅴ流畅 | 亚洲尤物av| 日本亚州欧洲精品不卡| 日韩国产欧美视频| 日韩成人精品一区二区三区| 综合在线一区| 日韩av不卡一区二区| 日本欧美韩国一区三区| 国产精品网站在线看| 国产精品嫩模av在线| 国产乱码精品一区二区三区亚洲人| 中文字幕日韩亚洲| 欧美一区自拍| 老色鬼精品视频在线观看播放| 精品高清久久| 高清一区二区| 婷婷精品视频| 综合激情网...| 国产精品一区二区中文字幕| 久久69成人| 99精品视频在线观看免费播放| 樱桃成人精品视频在线播放| 亚洲伊人精品酒店| 国产剧情在线观看一区| 日韩在线看片| 久久国产精品久久久久久电车 | 欧美日韩视频免费观看| re久久精品视频| 视频一区二区三区中文字幕| 日本a级不卡| 国语精品一区| 午夜国产欧美理论在线播放| 蜜臀av亚洲一区中文字幕| 国产精品红桃| se01亚洲视频 | 国产精品jk白丝蜜臀av小说| 国产精品二区不卡| 国产精品美女久久久| 欧美在线看片| 91精品蜜臀一区二区三区在线| 久久不射网站| 免费一区二区三区在线视频| 欧洲一级精品| 中文一区一区三区免费在线观 | 久久免费视频66| 女人av一区| 国产日韩三级| 精品欧美久久| 国产精品黄色片| 亚洲黄页一区| 久久尤物视频| 欧美中文字幕| 国产黄大片在线观看| 亚洲综合电影一区二区三区| 色综合视频一区二区三区日韩| 国产精品久久久久久久久久10秀 | 青青草视频一区| 久久久人人人| 国产欧美久久一区二区三区| 九一精品国产| 国产精品久久久久久久免费软件| 激情综合自拍| 捆绑调教美女网站视频一区| 免费视频久久| 久久久久久久久丰满| 国产亚洲电影| 欧美日韩日本国产亚洲在线| 久久a爱视频| 中文视频一区| 国产一区日韩一区| 久久亚洲资源中文字| 视频一区在线播放| 欧美精品资源| 久久91视频| 日韩福利视频一区| 99国产精品| 亚洲成a人片| 欧美精品aa| 婷婷成人av| 亚洲欧美视频| 欧美日韩高清| av在线最新| 你懂的国产精品| 日韩激情精品| 亚洲一区二区三区久久久| 99久久精品网站| 国产一区二区三区国产精品| 7m精品国产导航在线| 丝袜美腿成人在线| 精品欧美久久| 九九精品调教| 亚洲女同av| 精品视频在线观看网站| 久久国产精品色av免费看| 午夜一级在线看亚洲| 色天使综合视频| 国产精品蜜月aⅴ在线| 91麻豆精品| 日本伊人久久| 免费欧美在线视频| 亚洲欧美久久久| 欧美日韩国产亚洲一区| 久久九九精品| 特黄毛片在线观看| 久久精品国产免费| 美女视频一区在线观看| 国产免费久久| 国产毛片精品| 欧美激情三区| 蜜桃精品视频| 精品久久网站| 欧美自拍一区| 国产欧美一区| 国产精品极品在线观看| 久久av免费| 美女国产一区二区三区| 91麻豆精品| 国产精品大片| 精品国产乱码| 美女一区网站| 日韩精品第一区| 91看片一区| 久久在线视频免费观看| 人在线成免费视频| 亚洲不卡系列| 亚洲免费黄色| 亚洲欧美日韩精品一区二区 | 精品成av人一区二区三区| 精品一区二区三区在线观看视频| 国产精品色在线网站| 美女视频黄 久久| 日韩av自拍| 日韩av一级| 香蕉国产精品| 亚洲精品麻豆| 国产精品一区二区三区www| 欧美自拍一区| 精品网站999| 私拍精品福利视频在线一区| 激情综合自拍| 伊人国产精品| 日韩美女国产精品| 国产精品成人一区二区网站软件| 毛片不卡一区二区| 在线中文字幕播放| 美女少妇全过程你懂的久久| 丝袜美腿亚洲色图| 国产亚洲一卡2卡3卡4卡新区| 欧美日韩一区自拍| 国产成人精品一区二区免费看京 | 丝袜美腿一区| 中文一区二区| 国产一卡不卡| 亚洲永久av| 伊人久久婷婷| 69堂精品视频在线播放| 国产精品二区不卡| 精品一区在线| 88久久精品| 日本а中文在线天堂| 黑丝一区二区三区| 亚洲精品一级| 麻豆91精品视频| 国产99亚洲| 日本午夜精品久久久久| 国产精品久久久久久久免费观看 | 成人在线免费观看网站| 免费久久久久久久久| 青青伊人久久| se01亚洲视频| 日本伊人久久| 夜鲁夜鲁夜鲁视频在线播放| 日韩制服丝袜av| 麻豆中文一区二区| 1024精品久久久久久久久| 91久久精品无嫩草影院| 日韩中文在线电影| 91免费精品国偷自产在线在线| 欧美一区久久久| 日欧美一区二区| 久久国产电影| 国产精品欧美大片| 亚洲尤物在线| 精品一区二区三区中文字幕在线| 99香蕉国产精品偷在线观看| 麻豆一区二区三| 首页国产欧美久久| 日韩a一区二区| 日本亚洲最大的色成网站www| 成人国产精品| 国产午夜久久av| 久久亚洲色图| 久久久久欧美精品| 久久精品国产福利| 午夜精品影视国产一区在线麻豆|