国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

常用的線程池介紹

2022-08-17 17:55 作者:限量版范兒  | 我要投稿

線程池:

簡介:線程池是用來統(tǒng)一管理線程的,在 Java 中創(chuàng)建和銷毀線程都是一件消耗資源的事情,線程池可以重復(fù)使用線程,不再頻繁的創(chuàng)建、銷毀線程。

線程池的作用是提高系統(tǒng)的性能和線程的利用率,不再需要頻繁的創(chuàng)建和銷毀線程。如果使用最簡單的方式創(chuàng)建線程,在用戶量巨大的情況下,消耗的性能是非??植赖模圆庞辛司€程池。

ThreadPoolExecutor

?

  • Executor: 代表線程池的接口,有一個 execute() 方法,給一個 Runnable 類型對象就可以分配一個線程執(zhí)行。

void execute(Runnablecommand);

  • ExecutorService:是 Executor 的子接口,提供了線程池的一些生命周期方法。代表了一個線程池管理器。

?

  • ThreadPoolExecutor:一個線程池的實現(xiàn)類,可以通過調(diào)用 Executors 靜態(tài)工廠方法來創(chuàng)建線程池并返回一個 ExecutorService 對象。

?

?

從源碼中可以看出每個前三個構(gòu)造函數(shù)都調(diào)用了最后一個構(gòu)造函數(shù)。

public ThreadPoolExecutor(int corePoolSize, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?int maximumPoolSize, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?long keepAliveTime, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?TimeUnit unit, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?BlockingQueue<Runnable> workQueue, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?ThreadFactory threadFactory, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?RejectedExecutionHandler handler) { ? ?//省略代碼 ? ?}

仔細(xì)分析一下構(gòu)造參數(shù):

  1. corePoolSize:線程池里的核心線程數(shù)量,當(dāng)正在運行的線程數(shù)量小于核心線程數(shù)量,就創(chuàng)建一個核心線程。

  2. maximumPoolSize:線程池最多能放多少個線程。

  3. keepAliveTime:線程的閑置時間,當(dāng)線程池里面的線程數(shù)量大于 corePoolSize 的時候,多出來的線程在等待的時間之后會被釋放掉

  4. unit:keepAliveTime 的單位

  5. workQueue:一個阻塞隊列。

  6. threadFactory:通過這個工廠模式創(chuàng)建線程。

  7. handler:處理線程隊列滿了報錯的。

結(jié)合線程池的參數(shù)簡單的畫出線程池的工作模型。

?

當(dāng)線程池中的核心線程數(shù)量 corePoolSize 滿了,就會將任務(wù)先加入到任務(wù)隊列 workQueue 中。

執(zhí)行過程

常用的線程池

ThredadPoolExcutor

SingleThreadExecutor

單線程的線程池,里面就一個核心線程數(shù)。只有一個線程在跑。

public static ExecutorService newSingleThreadExecutor() { ? ?return new FinalizableDelegatedExecutorService ? ? ? ?(new ThreadPoolExecutor(1, 1, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0L, TimeUnit.MILLISECONDS, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new LinkedBlockingQueue<Runnable>())); }public class TodoDemo implements Runnable { ? ?public static void main(String[] args) { ? ? ? ?ExecutorService executorService = Executors.newSingleThreadExecutor(); ? ? ? ?for(int i = 0; i < 10; i++) { ? ? ? ? ? ?executorService.execute(new TodoDemo()); ? ? ? ?} ? ? ? ?executorService.shutdown(); ? ?} ? ?@Override ? ?public void run() { ? ? ? ?System.out.println(Thread.currentThread().getName() + " Running"); ? ?} }

?

FixedThreadExecutor

這個線程池的特點就是線程的數(shù)量是固定的,超過這個數(shù)量的任務(wù)就得在 LinkedBlockingQueue 中排隊等候。

??

return new ThreadPoolExecutor(nThreads, nThreads, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0L, TimeUnit.MILLISECONDS, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new LinkedBlockingQueue<Runnable>()); }public class TodoDemo implements Runnable { ? ? ?public static void main(String[] args) { ? ? ? ?ExecutorService executorService = Executors.newFixedThreadPool(3); ? ? ? ?for(int i = 0; i < 10; i++) { ? ? ? ? ? ?executorService.execute(new TodoDemo()); ? ? ? ?} ? ? ? ?executorService.shutdown(); ? ?} ? ?@Override ? ?public void run() { ? ? ? ?System.out.println(Thread.currentThread().getName() + " Running"); ? ?} }

?

CachedThreadExecutor

自動回收空閑的線程,核心線程數(shù)量為 0, 表示不會永久保留任何的線程,最大線程的數(shù)量是 Integer.MAX_VALUE,可以無限制的創(chuàng)建線程,但是當(dāng)有大量線程處于空閑狀態(tài)的時候,超過 60s 就會被銷毀。雖然這個線程池可以想建多少個線程就建多少個線程,但是還是會重用已經(jīng)完成任務(wù)的線程。

public static ExecutorService newCachedThreadPool() { ? ?return new ThreadPoolExecutor(0, Integer.MAX_VALUE, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?60L, TimeUnit.SECONDS, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new SynchronousQueue<Runnable>()); }public class TodoDemo implements Runnable { ? ?public static void main(String[] args) { ? ? ? ?ExecutorService executorService = Executors.newCachedThreadPool(); ? ? ? ?for(int i = 0; i < 20; i++) { ? ? ? ? ? ?executorService.execute(new TodoDemo()); ? ? ? ?} ? ? ? ?executorService.shutdown(); ? ?} ? ?@Override ? ?public void run() { ? ? ? ?System.out.println(Thread.currentThread().getName() + " Running"); ? ?} }

?

線程池的回收策略

在線程池中任務(wù)隊列已經(jīng)滿了,并且線程的數(shù)量已經(jīng)到了最大的數(shù)量,這個時候再加任務(wù)線程池就不再接受了。

在 ThreadPoolExecutor 里有 4 種拒絕策略,都實現(xiàn)了 RejectedExecutionHandler:

  1. AbortPolicy 表示拋出一個異常。

  2. DiscardPolicy 拒絕任務(wù)但是不提示。

  3. DiscardOldestPolicy 丟棄掉老的任務(wù),執(zhí)行新的任務(wù)。

  4. CallerRunsPolicy 直接調(diào)用線程處理。

鏈接:https://www.dianjilingqu.com/489540.html

常用的線程池介紹的評論 (共 條)

分享到微博請遵守國家法律
冀州市| 安陆市| 塔河县| 双城市| 唐河县| 蓝山县| 海南省| 昂仁县| 长沙县| 商丘市| 德兴市| 德化县| 池州市| 宁海县| 准格尔旗| 平阴县| 绍兴县| 克什克腾旗| 咸阳市| 彭山县| 高唐县| 松原市| 大化| 集安市| 门头沟区| 安塞县| 望城县| 湖州市| 循化| 鄂州市| 翁牛特旗| 瑞金市| 玛纳斯县| 枣阳市| 乐业县| 鹤岗市| 长顺县| 乌拉特前旗| 新干县| 贡山| 田阳县|