摘要:系統(tǒng)預定了幾個線程池,不過建議手動創(chuàng)建,以防止錯誤創(chuàng)建消耗資源,比如創(chuàng)建太多線程或者固定線程數(shù)量,無界隊列固定線程數(shù)量,數(shù)量為,無界隊列,會按順序執(zhí)行不限制線程數(shù)量,使用隊列,使用于短任務基于用于周期性執(zhí)行任務示例第一個是,第二個是第一
系統(tǒng)預定了幾個線程池,不過建議手動創(chuàng)建,以防止錯誤創(chuàng)建消耗資源,比如創(chuàng)建太多線程或者OOM
FixedThreadPool固定線程數(shù)量,無界隊列
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueueSingleThreadExecutor()); }
固定線程數(shù)量,數(shù)量為1,無界隊列,會按順序執(zhí)行
public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueueCachedThreadPool())); }
不限制線程數(shù)量,使用SynchronousQueue隊列,使用于短任務
public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueueWorkStealingPool()); }
基于ForkJoinPool
public static ExecutorService newWorkStealingPool(int parallelism) { return new ForkJoinPool (parallelism, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); }ScheduledThreadPoolExecutor
用于周期性執(zhí)行任務
public static ScheduledExecutorService newSingleThreadScheduledExecutor() { return new DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1)); }示例
public class ScheduledDemo { static class Thread1 implements Runnable { @Override public void run() { SimpleDateFormat formater = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); System.out.println(Thread.currentThread().getName() + ":" + formater.format(new Date())); } } public static void main(String[] args) { ScheduledThreadPoolExecutor schedule = new ScheduledThreadPoolExecutor(1); //第一個是Runnable,第二個是第一次開始的時間,第三個是周期時間,第四個是時間單位 schedule.scheduleAtFixedRate(new Thread1(),1000,1000, TimeUnit.MILLISECONDS); } }
運行結(jié)果如下:
文章版權歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/75723.html
摘要:是所有線程池實現(xiàn)的父類,我們先看看構(gòu)造函數(shù)構(gòu)造參數(shù)線程核心數(shù)最大線程數(shù)線程空閑后,存活的時間,只有線程數(shù)大于的時候生效存活時間的單位任務的阻塞隊列創(chuàng)建線程的工程,給線程起名字當線程池滿了,選擇新加入的任務應該使用什么策略,比如拋異常丟棄當前 ThreadPoolExecutor ThreadPoolExecutor是所有線程池實現(xiàn)的父類,我們先看看構(gòu)造函數(shù) 構(gòu)造參數(shù) corePool...
摘要:接口用于提交任務接口繼承了接口設置線程的狀態(tài),還沒執(zhí)行的線程會被中斷設置線程的狀態(tài),嘗試停止正在進行的線程當調(diào)用或方法后返回為當調(diào)用方法后,并且所有提交的任務完成后返回為當調(diào)用方法后,成功停止后返回為當前線程阻塞,直到線程執(zhí)行完時間到被中斷 Executor接口 void execute(Runnable command)//用于提交command任務 ExecutorService接...
摘要:抽象類,實現(xiàn)了的接口。將任務封裝成提交任務主要方法在任務是否超時超時時間任務書用于存放結(jié)果的,先完成的放前面。 AbstractExecutorService抽象類,實現(xiàn)了ExecutorService的接口。 newTaskFor 將任務封裝成FutureTask protected RunnableFuture newTaskFor(Runnable runnable, T va...
摘要:定義等待該線程終止,比如線程調(diào)用了線程的,那么線程要等到線程執(zhí)行完后,才可以繼續(xù)執(zhí)行。 定義 等待該線程終止,比如A線程調(diào)用了B線程的join,那么A線程要等到B線程執(zhí)行完后,才可以繼續(xù)執(zhí)行。 示例 public class JoinDemo { static class JoinThread1 implements Runnable { Thread thre...
摘要:并且,線程池在某些情況下還能動態(tài)調(diào)整工作線程的數(shù)量,以平衡資源消耗和工作效率。同時線程池還提供了對池中工作線程進行統(tǒng)一的管理的相關方法。 開發(fā)中經(jīng)常會遇到各種池(如:連接池,線程池),它們的作用就是為了提高性能及減少開銷,在JDK1.5以后的java.util.concurrent包中內(nèi)置了很多不同使用場景的線程池,為了更好的理解它們,自己手寫一個線程池,加深印象。 概述 1.什么是...
閱讀 2707·2021-11-25 09:43
閱讀 2792·2021-11-04 16:09
閱讀 1785·2021-10-12 10:13
閱讀 934·2021-09-29 09:35
閱讀 939·2021-08-03 14:03
閱讀 1829·2019-08-30 15:55
閱讀 3063·2019-08-28 18:14
閱讀 3653·2019-08-26 13:43