提交时是使用防抖还是节流

JDK的java.util.concurrent.ThreadPoolExecutor允许您将任务提交到线程池,并使用BlockingQueue来保存提交的任务。 如果要提交的任务有数千个,请指定一个“绑定”队列(即最大容量的队列),否则JVM可能会用完内存。 您可以设置RejectedExecutionHandler来处理队列已满时发生的情况,但是仍然有待提交的任务。 这里是你展示如何使用一个简单的例子ThreadPoolExecutor具有BlockingQueue容量1000 CallerRunsPolicy确保,当队列已满时,其他任务将由提交线程处理。

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

这种方法的问题在于,当队列已满时,向池提交任务的线程会变得忙于执行任务本身,在此期间,队列可能会变空并且池中的线程可能会变得空闲。 这不是很有效。 我们希望一直保持线程池繁忙,并且工作队列始终处于饱和状态。 有各种解决方案。 其中之一是使用自定义的Executor ,当队列已满时,该Executor将阻止(从而防止其他任务提交到池中)。 BlockingExecutor的代码如下所示。 它基于Brian Goetz,2006年的BoundedExecutor示例。Java Concurrency in Practice。 1版。 Addison-Wesley专业。 (第8.3.3节) 。

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进行节流任务提交 。

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

提交时是使用防抖还是节流

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

  1. eclipse中使用git提交时忽略不必要的文件

    @ eclipse中使用git提交时忽略不必要的文件 我们在eclipse中使用git提交代码时,如果没有添加任何过滤条件,那么会把所有文件都提交.但是实际上有些文件/目录并不需要提交,比如targe ...

  2. SVN可以拉项目但是提交时提示没有权限

    我SVN是有权限的,而且也能从服务器上拉下来项目,但就是提交的时候提示403没有权限. 最后通过百度经验和翻找博客解决. 原因是路径问题: SVN在拉取项目的时候不区分大小写,但是在提交时是严格区分大 ...

  3. SVN、Git设置提交时忽略的文件

    个人正在使用的:global-ignores = *.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.pyc *.pyo [Tt]humbs.db [Bb]in [ ...

  4. Git提交时提示‘The file will have its original line endings in your working directory’

    Git提交时提示'The file will have its original line endings in your working directory' Git出现错误 git add -A ...

  5. git提交时支持文件名大小写的修改

    在windows环境下,git提交文件时,默认对文件名大小写不敏感,若修改了文件名字的大小写,可能会导致提交时没有记录,文件名修改不成功.网上搜集了几种解决方法,现总结下: www.cnblogs.c ...

  6. linux svn强制注释,svn强制提交时添加注释

    svn强制提交时添加注释 1.  需求 领导要求开发在提交代码时必须添加注释,经过查资料,实验,然后实现领导的要求. 2.  查看资料 参考svn文档,以及网上博客 通过查看资料,一步一步的实现脚本 ...

  7. Git提交时关于vim的错误

    在 Git Bash 中使用 Vim 提交时,会报错,详情如下: $ git commit -v 复制代码 Error: There was a problem with the editor 'vi ...

  8. BJUI使用ajax异步表单提交时后台action要加@ResponseBody

    场景 在使用BJUI的ajaxform异步表单提交时,后台直接使用 @RequestMapping时不能正常回显消息. 知识储备 ajaxform异步表单提交 通过data属性使用(带验证): < ...

  9. SVN提交时强制用户写日志

    在使用SVN进行项目管理的过程中,为了对各版本提交状况进行了解,我们需要在SVN提交过程中强制用户输入一定的日志. 下面介绍一下如何来限制用户SVN提交时必须输入日志. 步骤: 1.进入SVN仓库的h ...

最新文章

  1. java web 心跳机制实现,基于javax的websocket服务端实现,含心跳机制
  2. python GIL :全局解释器
  3. 怎么查交集_胃镜要不要查?
  4. centos php redhat,RHEL / CentOS 安装 OPcache 提升 PHP 效能
  5. php支持 的编码,php编码转换函数(自动转换字符集支持数组转换)
  6. HDOJ 1285 确定比赛名次(拓扑排序)
  7. 数据结构与算法——二叉排序树详解以及代码实现
  8. Cache计算的再总结
  9. Mysql源码编译安装主从复制
  10. 微信小程序动画效果集合
  11. Python cx_Oracle执行的sql字符串拼接含分号导致报“ORA-01756“引号内的字符串没有正确结束
  12. 全国计算机考试一级在线模拟,全国计算机等级考试一级模拟试题和答案
  13. Spring框架学习笔记
  14. 三维空间的位姿描述和齐次变换
  15. 【转】《仙剑奇侠传6》将更换全新引擎
  16. webpack打包路径更改_webpack打包教程
  17. 华为路由器负载均衡_华为OSPF路由负载分担配置示例
  18. 如何将录音转成文字?
  19. 对称矩阵的三对角分解(Lanzos分解算法)-MINRES算法预热
  20. java面试(3)SQL优化

热门文章

  1. 初一模拟赛总结(2019.6.1)
  2. 【并查集】团伙(luogu 1892)
  3. 17、java中的集合(4)
  4. sql中索引不会被用到的几种情况
  5. MySQL extract()函数
  6. Java程序员最常犯的 10 个错误
  7. Oracle入门(十二D)之表删除与删除表数据
  8. Java8系列之重新认识HashMap
  9. 高级java必须清楚的概念:原子性、可见性、有序性
  10. java程序员被误导的一个概念,Set也可以有序