這篇文章主要講解了Java中有界隊列的飽和策略的用法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
博白ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
我們在使用ExecutorService的時候知道,在ExecutorService中有個一個Queue來保存提交的任務,通過不同的構造函數(shù),我們可以創(chuàng)建無界的隊列(ExecutorService.newCachedThreadPool)和有界的隊列(ExecutorService newFixedThreadPool(int nThreads))。
無界隊列很好理解,我們可以無限制的向ExecutorService提交任務。那么對于有界隊列來說,如果隊列滿了該怎么處理呢?
今天我們要介紹一下java中ExecutorService的飽和策略(reject policy)。
以ExecutorService的具體實現(xiàn)ThreadPoolExecutor來說,它定義了4種飽和策略。分別是AbortPolicy,DiscardPolicy,DiscardOldestPolicy和CallerRunsPolicy。
如果要在ThreadPoolExecutor中設定飽和策略可以調(diào)用setRejectedExecutionHandler方法,如下所示:
ThreadPoolExecutor threadPoolExecutor= new ThreadPoolExecutor(5, 10, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(20)); threadPoolExecutor.setRejectedExecutionHandler( new ThreadPoolExecutor.AbortPolicy() );
上面的例子中我們定義了一個初始5個,最大10個工作線程的Thread Pool,并且定義其中的Queue的容量是20。如果提交的任務超出了容量,則會使用AbortPolicy策略。
AbortPolicy
AbortPolicy意思是如果隊列滿了,最新的提交任務將會被拒絕,并拋出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()); } }
上面的代碼中,rejectedExecution方法中我們直接拋出了RejectedExecutionException異常。
DiscardPolicy
DiscardPolicy將會悄悄的丟棄提交的任務,而不報任何異常。
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
DiscardOldestPolicy將會丟棄最老的任務,保存最新插入的任務。
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); } } }
我們看到在rejectedExecution方法中,poll了最老的一個任務,然后使用ThreadPoolExecutor提交了一個最新的任務。
CallerRunsPolicy
CallerRunsPolicy和其他的幾個策略不同,它既不會拋棄任務,也不會拋出異常,而是將任務回退給調(diào)用者,使用調(diào)用者的線程來執(zhí)行任務,從而降低調(diào)用者的調(diào)用速度。我們看下是怎么實現(xiàn)的:
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(); } } }
在rejectedExecution方法中,直接調(diào)用了 r.run()方法,這會導致該方法直接在調(diào)用者的主線程中執(zhí)行,而不是在線程池中執(zhí)行。從而導致主線程在該任務執(zhí)行結束之前不能提交任何任務。從而有效的阻止了任務的提交。
使用Semaphore
如果我們并沒有定義飽和策略,那么有沒有什么方法來控制任務的提交速度呢?考慮下之前我們講到的Semaphore,我們可以指定一定的資源信號量來控制任務的提交,如下所示:
public class SemaphoreUsage { private final Executor executor; private final Semaphore semaphore; public SemaphoreUsage(Executor executor, int count) { this.executor = executor; this.semaphore = new Semaphore(count); } public void submitTask(final Runnable command) throws InterruptedException { semaphore.acquire(); try { executor.execute(() -> { try { command.run(); } finally { semaphore.release(); } } ); } catch (RejectedExecutionException e) { semaphore.release(); } } }
看完上述內(nèi)容,是不是對Java中有界隊列的飽和策略的用法有進一步的了解,如果還想學習更多內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章名稱:Java中有界隊列的飽和策略的用法
分享URL:http://sd-ha.com/article48/jocpep.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、虛擬主機、定制開發(fā)、自適應網(wǎng)站、、關鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)