java高级应用:线程池全面解析
?
什么是線程池?
很簡單,簡單看名字就知道是裝有線程的池子,我們可以把要執行的多線程交給線程池來處理,和連接池的概念一樣,通過維護一定數量的線程池來達到多個線程的復用。
?
線程池的好處
我們知道不用線程池的話,每個線程都要通過new Thread(xxRunnable).start()的方式來創建并運行一個線程,線程少的話這不會是問題,而真實環境可能會開啟多個線程讓系統和程序達到最佳效率,當線程數達到一定數量就會耗盡系統的CPU和內存資源,也會造成GC頻繁收集和停頓,因為每次創建和銷毀一個線程都是要消耗系統資源的,如果為每個任務都創建線程這無疑是一個很大的性能瓶頸。所以,線程池中的線程復用極大節省了系統資源,當線程一段時間不再有任務處理時它也會自動銷毀,而不會長駐內存。
?
線程池核心類
在java.util.concurrent包中我們能找到線程池的定義,其中ThreadPoolExecutor是我們線程池核心類,首先看看線程池類的主要參數有哪些。
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler) {if (corePoolSize < 0 ||maximumPoolSize <= 0 ||maximumPoolSize < corePoolSize ||keepAliveTime < 0)throw new IllegalArgumentException();if (workQueue == null || threadFactory == null || handler == null)throw new NullPointerException();this.corePoolSize = corePoolSize;this.maximumPoolSize = maximumPoolSize;this.workQueue = workQueue;this.keepAliveTime = unit.toNanos(keepAliveTime);this.threadFactory = threadFactory;this.handler = handler;}-
corePoolSize:線程池的核心大小,也可以理解為最小的線程池大小。
-
maximumPoolSize:最大線程池大小。
-
keepAliveTime:空余線程存活時間,指的是超過corePoolSize的空余線程達到多長時間才進行銷毀。
-
unit:銷毀時間單位。
-
workQueue:存儲等待執行線程的工作隊列。
-
threadFactory:創建線程的工廠,一般用默認即可。
-
handler:拒絕策略,當工作隊列、線程池全已滿時如何拒絕新任務,默認拋出異常。
?
線程池工作流程
1、如果線程池中的線程小于corePoolSize時就會創建新線程直接執行任務。
2、如果線程池中的線程大于corePoolSize時就會暫時把任務存儲到工作隊列workQueue中等待執行。
3、如果工作隊列workQueue也滿時:當線程數小于最大線程池數maximumPoolSize時就會創建新線程來處理,而線程數大于等于最大線程池數maximumPoolSize時就會執行拒絕策略。
?
線程池分類
Executors是jdk里面提供的創建線程池的工廠類,它默認提供了4種常用的線程池應用,而不必我們去重復構造。
-
newFixedThreadPool
固定線程池,核心線程數和最大線程數固定相等,而空閑存活時間為0毫秒,說明此參數也無意義,工作隊列為最大為Integer.MAX_VALUE大小的阻塞隊列。當執行任務時,如果線程都很忙,就會丟到工作隊列等有空閑線程時再執行,隊列滿就執行默認的拒絕策略。
?
-
newCachedThreadPool
? ? ? ? 帶緩沖線程池,從構造看核心線程數為0,最大線程數為Integer最大值大小,超過0個的空閑線程在60秒后銷毀,SynchronousQueue這是一個直接提交的隊列,意味著每個新任務都會有線程來執行,如果線程池有可用線程則執行任務,沒有的話就創建一個來執行,線程池中的線程數不確定,一般建議執行速度較快較小的線程,不然這個最大線程池邊界過大容易造成內存溢出。
/*** 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 {@code execute} 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>());}?
-
newSingleThreadExecutor
????????單線程線程池,核心線程數和最大線程數均為1,空閑線程存活0毫秒同樣無意思,意味著每次只執行一個線程,多余的先存儲到工作隊列,一個一個執行,保證了線程的順序執行。
/*** Creates an Executor that uses a single worker thread operating* off an unbounded queue. (Note however that if this single* thread terminates due to a failure during execution prior to* shutdown, a new one will take its place if needed to execute* subsequent tasks.) Tasks are guaranteed to execute* sequentially, and no more than one task will be active at any* given time. Unlike the otherwise equivalent* {@code newFixedThreadPool(1)} the returned executor is* guaranteed not to be reconfigurable to use additional threads.** @return the newly created single-threaded Executor*/public static ExecutorService newSingleThreadExecutor() {return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>()));}?
-
newScheduledThreadPool
調度線程池,即按一定的周期執行任務,即定時任務,對ThreadPoolExecutor進行了包裝而已。
?
拒絕策略
-
AbortPolicy
??????簡單粗暴,直接拋出拒絕異常,這也是默認的拒絕策略。
/*** A handler for rejected tasks that throws a* {@code RejectedExecutionException}.*/public static class AbortPolicy implements RejectedExecutionHandler {/*** Creates an {@code AbortPolicy}.*/public AbortPolicy() { }/*** Always throws RejectedExecutionException.** @param r the runnable task requested to be executed* @param e the executor attempting to execute this task* @throws RejectedExecutionException always*/public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {throw new RejectedExecutionException("Task " + r.toString() +" rejected from " +e.toString());}} /*** The default rejected execution handler*/private static final RejectedExecutionHandler defaultHandler =new AbortPolicy();?
-
CallerRunsPolicy? ? ?
???????如果線程池未關閉,則會在調用者線程中直接執行新任務,這會導致主線程提交線程性能變慢。
/*** A handler for rejected tasks that runs the rejected task* directly in the calling thread of the {@code execute} method,* unless the executor has been shut down, in which case the task* is discarded.*/public static class CallerRunsPolicy implements RejectedExecutionHandler {/*** Creates a {@code CallerRunsPolicy}.*/public CallerRunsPolicy() { }/*** Executes task r in the caller's thread, unless the executor* has been shut down, in which case the task is discarded.** @param r the runnable task requested to be executed* @param e the executor attempting to execute this task*/public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {if (!e.isShutdown()) {r.run();}}}?
-
DiscardPolicy
????? ?從方法看沒做任務操作,即表示不處理新任務,即丟棄。
/*** A handler for rejected tasks that silently discards the* rejected task.*/public static class DiscardPolicy implements RejectedExecutionHandler {/*** Creates a {@code DiscardPolicy}.*/public DiscardPolicy() { }/*** Does nothing, which has the effect of discarding task r.** @param r the runnable task requested to be executed* @param e the executor attempting to execute this task*/public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {}}?
-
DiscardOldestPolicy
????? ?拋棄最老的任務,就是從隊列取出最老的任務然后放入新的任務進行執行。? ? ? ??
/*** A handler for rejected tasks that discards the oldest unhandled* request and then retries {@code execute}, unless the executor* is shut down, in which case the task is discarded.*/public static class DiscardOldestPolicy implements RejectedExecutionHandler {/*** Creates a {@code DiscardOldestPolicy} for the given executor.*/public DiscardOldestPolicy() { }/*** Obtains and ignores the next task that the executor* would otherwise execute, if one is immediately available,* and then retries execution of task r, unless the executor* is shut down, in which case task r is instead discarded.** @param r the runnable task requested to be executed* @param e the executor attempting to execute this task*/public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {if (!e.isShutdown()) {e.getQueue().poll();e.execute(r);}}}?
如何提交線程
如可以先隨便定義一個固定大小的線程池
ExecutorService es = Executors.newFixedThreadPool(3);提交一個線程
es.submit(xxRunnble); es.execute(xxRunnble);submit和execute分別有什么區別呢?
execute沒有返回值,如果不需要知道線程的結果就使用execute方法,性能會好很多。
submit返回一個Future對象,如果想知道線程結果就使用submit提交,而且它能在主線程中通過Future的get方法捕獲線程中的異常。
?
如何關閉線程池
es.shutdown();?不再接受新的任務,之前提交的任務等執行結束再關閉線程池。
?
es.shutdownNow();不再接受新的任務,試圖停止池中的任務再關閉線程池,返回所有未處理的線程list列表。
總結
以上是生活随笔為你收集整理的java高级应用:线程池全面解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: volatile关键字解析~高级java
- 下一篇: 并发控制--悲观锁和乐观锁详解