做者:天才程序YUAN」
原文:https://blog.csdn.net/JAck_chen0309/article/details/105250643java
start()建立必定數量的線程池,進行線程循環
stop()中止全部線程循環,回收全部資源
addTask()添加任務複製代碼
Executors.newFixedThreadPool(100);//建立固定大小的線程池
Executors.newSingleThreadExecutor();//建立只有一個線程的線程池
Executors.newCachedThreadPool();//建立一個不限線程數上限的線程池,任何提交的任務都將當即執行複製代碼
public ThreadPoolExecutor(
int corePoolPoolSize,//線程池長期維持的線程數
int maximumPoolSize, //線程數的上限
long keepAliveTime,//空閒線程存活時間
TimeUnit unit,//時間單位
BlockingQueue<Runnable> workQueue,//任務的排隊隊列
ThreadFactory threadFactory,//新線程的產生方式
RejectedExecutionHandler handler//拒絕策略
)複製代碼
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
複製代碼
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
複製代碼
(1)Executors.newFixedThreadPool(int nThreads);//nThreads爲線程的數量
(2)Executors.newFixedThreadPool(int nThreads,ThreadFactory threadFactory);//nThreads爲線程的數量,threadFactory建立線程的工廠方式複製代碼
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
複製代碼
(1)Executors.newSingleThreadExecutor() ;
(2)Executors.newSingleThreadExecutor(ThreadFactory threadFactory);// threadFactory建立線程的工廠方式複製代碼
public class ScheduledThreadPoolExecutor
extends ThreadPoolExecutor
implements ScheduledExecutorService {
....................
/**
* Creates a new {@code ScheduledThreadPoolExecutor} with the
* given core pool size.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @throws IllegalArgumentException if {@code corePoolSize < 0}
*/
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
複製代碼
(1)Executors.newScheduledThreadPool(int corePoolSize);// corePoolSize線程的個數
(2)newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory);// corePoolSize線程的個數,threadFactory建立線程的工廠複製代碼
(1)Executors.newSingleThreadScheduledExecutor() ;
(2)Executors.newSingleThreadScheduledExecutor(ThreadFactory threadFactory);//threadFactory建立線程的工廠複製代碼
DiscardPolicy:不能執行的任務將被刪除程序員
DiscardOldestPolicy:若是執行程序還沒有關閉,則位於工做隊列頭部的任務將被刪除,而後重試執行程序(若是再次失敗,則重複此過程)面試
CallerRunsPolicy:線程調用運行該任務的 execute 自己。此策略提供簡單的反饋控制機制,可以減緩新任務的提交速度。算法
RejectedExecutionHandler rejected = null;
rejected = new ThreadPoolExecutor.AbortPolicy();//默認,隊列滿了丟任務拋出異常
rejected = new ThreadPoolExecutor.DiscardPolicy();//隊列滿了丟任務不異常
rejected = new ThreadPoolExecutor.DiscardOldestPolicy();//將最先進入隊列的任務刪,以後再嘗試加入隊列
rejected = new ThreadPoolExecutor.CallerRunsPolicy();//若是添加到線程池失敗,那麼主線程會本身去執行該任務複製代碼
歡迎關注公衆號:程序員追風,領取一線大廠Java面試題總結+各知識點學習思惟導+一份300頁pdf文檔的Java核心知識點總結!spring
這些資料的內容都是面試時面試官必問的知識點,篇章包括了不少知識點,其中包括了有基礎知識、Java集合、JVM、多線程併發、spring原理、微服務、Netty 與RPC 、Kafka、日記、設計模式、Java算法、數據庫、Zookeeper、分佈式緩存、數據結構等等。數據庫