日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

提交时是使用防抖还是节流_使用BlockingExecutor进行节流任务提交

發(fā)布時間:2023/12/3 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 提交时是使用防抖还是节流_使用BlockingExecutor进行节流任务提交 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

提交時是使用防抖還是節(jié)流

JDK的java.util.concurrent.ThreadPoolExecutor允許您將任務(wù)提交到線程池,并使用BlockingQueue來保存提交的任務(wù)。 如果要提交的任務(wù)有數(shù)千個,請指定一個“綁定”隊列(即最大容量的隊列),否則JVM可能會用完內(nèi)存。 您可以設(shè)置RejectedExecutionHandler來處理隊列已滿時發(fā)生的情況,但是仍然有待提交的任務(wù)。 這里是你展示如何使用一個簡單的例子ThreadPoolExecutor具有BlockingQueue容量1000 CallerRunsPolicy確保,當(dāng)隊列已滿時,其他任務(wù)將由提交線程處理。

int numThreads = 5; ExecutorService exec = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1000),new ThreadPoolExecutor.CallerRunsPolicy());

這種方法的問題在于,當(dāng)隊列已滿時,向池提交任務(wù)的線程會變得忙于執(zhí)行任務(wù)本身,在此期間,隊列可能會變空并且池中的線程可能會變得空閑。 這不是很有效。 我們希望一直保持線程池繁忙,并且工作隊列始終處于飽和狀態(tài)。 有各種解決方案。 其中之一是使用自定義的Executor ,當(dāng)隊列已滿時,該Executor將阻止(從而防止其他任務(wù)提交到池中)。 BlockingExecutor的代碼如下所示。 它基于Brian Goetz,2006年的BoundedExecutor示例。Java Concurrency in Practice。 1版。 Addison-Wesley專業(yè)。 (第8.3.3節(jié)) 。

import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.Semaphore; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit;import org.slf4j.Logger; import org.slf4j.LoggerFactory;/*** An executor which blocks and prevents further tasks from* being submitted to the pool when the queue is full.* <p>* Based on the BoundedExecutor example in:* Brian Goetz, 2006. Java Concurrency in Practice. (Listing 8.4)*/ public class BlockingExecutor extends ThreadPoolExecutor {private static final Logger LOGGER = LoggerFactory.getLogger(BlockingExecutor.class);private final Semaphore semaphore;/*** Creates a BlockingExecutor which will block and prevent further* submission to the pool when the specified queue size has been reached.** @param poolSize the number of the threads in the pool* @param queueSize the size of the queue*/public BlockingExecutor(final int poolSize, final int queueSize) {super(poolSize, poolSize, 0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());// the semaphore is bounding both the number of tasks currently executing// and those queued upsemaphore = new Semaphore(poolSize + queueSize);}/*** Executes the given task.* This method will block when the semaphore has no permits* i.e. when the queue has reached its capacity.*/@Overridepublic void execute(final Runnable task) {boolean acquired = false;do {try {semaphore.acquire();acquired = true;} catch (final InterruptedException e) {LOGGER.warn("InterruptedException whilst aquiring semaphore", e);}} while (!acquired);try {super.execute(task);} catch (final RejectedExecutionException e) {semaphore.release();throw e;}}/*** Method invoked upon completion of execution of the given Runnable,* by the thread that executed the task.* Releases a semaphore permit.*/@Overrideprotected void afterExecute(final Runnable r, final Throwable t) {super.afterExecute(r, t);semaphore.release();} }

參考:我們的JCG合作伙伴 Fahd Shariff在fahd.blog博客上使用BlockingExecutor進行節(jié)流任務(wù)提交 。

翻譯自: https://www.javacodegeeks.com/2013/11/throttling-task-submission-with-a-blockingexecutor.html

提交時是使用防抖還是節(jié)流

總結(jié)

以上是生活随笔為你收集整理的提交时是使用防抖还是节流_使用BlockingExecutor进行节流任务提交的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。