提交时是使用防抖还是节流_使用BlockingExecutor进行节流任务提交
提交時是使用防抖還是節(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ù)將由提交線程處理。
這種方法的問題在于,當(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软平板电脑系统激活(微软官方系统激活)
- 下一篇: 古希腊罗马神话的特点(古希腊罗马神话人物