官方文档:

/*** Support class for throttling concurrent access to a specific resource.** <p>Designed for use as a base class, with the subclass invoking* the {@link #beforeAccess()} and {@link #afterAccess()} methods at* appropriate points of its workflow. Note that {@code afterAccess}* should usually be called in a finally block!** <p>The default concurrency limit of this support class is -1* ("unbounded concurrency"). Subclasses may override this default;* check the javadoc of the concrete class that you're using.** @author Juergen Hoeller* @since 1.2.5* @see #setConcurrencyLimit* @see #beforeAccess()* @see #afterAccess()* @see org.springframework.aop.interceptor.ConcurrencyThrottleInterceptor* @see java.io.Serializable*/

beforeAccess()实现

/*** To be invoked before the main execution logic of concrete subclasses.* <p>This implementation applies the concurrency throttle.* @see #afterAccess()*/protected void beforeAccess() {if (this.concurrencyLimit == NO_CONCURRENCY) {throw new IllegalStateException("Currently no invocations allowed - concurrency limit set to NO_CONCURRENCY");}if (this.concurrencyLimit > 0) {boolean debug = logger.isDebugEnabled();synchronized (this.monitor) {boolean interrupted = false;while (this.concurrencyCount >= this.concurrencyLimit) {if (interrupted) {throw new IllegalStateException("Thread was interrupted while waiting for invocation access, " +"but concurrency limit still does not allow for entering");}if (debug) {logger.debug("Concurrency count " + this.concurrencyCount +" has reached limit " + this.concurrencyLimit + " - blocking");}try {this.monitor.wait();}catch (InterruptedException ex) {// Re-interrupt current thread, to allow other threads to react.
                        Thread.currentThread().interrupt();interrupted = true;}}if (debug) {logger.debug("Entering throttle at concurrency count " + this.concurrencyCount);}this.concurrencyCount++;}}}

afterAccess()实现

    /*** To be invoked after the main execution logic of concrete subclasses.* @see #beforeAccess()*/protected void afterAccess() {if (this.concurrencyLimit >= 0) {synchronized (this.monitor) {this.concurrencyCount--;if (logger.isDebugEnabled()) {logger.debug("Returning from throttle at concurrency count " + this.concurrencyCount);}this.monitor.notify();}}}

ConcurrencyThrottleSupport是个抽象类,其具体的实现类ConcurrencyThrottleInterceptor

/*** Interceptor that throttles concurrent access, blocking invocations* if a specified concurrency limit is reached.** <p>Can be applied to methods of local services that involve heavy use* of system resources, in a scenario where it is more efficient to* throttle concurrency for a specific service rather than restricting* the entire thread pool (e.g. the web container's thread pool).** <p>The default concurrency limit of this interceptor is 1.* Specify the "concurrencyLimit" bean property to change this value.** @author Juergen Hoeller* @since 11.02.2004* @see #setConcurrencyLimit*/
@SuppressWarnings("serial")
public class ConcurrencyThrottleInterceptor extends ConcurrencyThrottleSupportimplements MethodInterceptor, Serializable {public ConcurrencyThrottleInterceptor() {setConcurrencyLimit(1);}@Overridepublic Object invoke(MethodInvocation methodInvocation) throws Throwable {beforeAccess();try {return methodInvocation.proceed();}finally {afterAccess();}}}

转载于:https://www.cnblogs.com/davidwang456/p/5998254.html

spring控制并发数的工具类ConcurrencyThrottleSupport和ConcurrencyThrottleInterceptor相关推荐

  1. 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:宇的季节 cnblogs.com/chenkeyu/p/80 ...

  2. 应用启动图标未读消息数显示 工具类

    /* * 应用启动图标未读消息数显示 工具类 (效果如:QQ.微信.未读短信 等应用图标) * */ public class BadgeUtil { /** * Set badge count * ...

  3. Spring 自带的一些工具类

    断言 断言是一个逻辑判断,用于检查不应该发生的情况 Assert 关键字在 JDK1.4 中引入,可通过 JVM 参数-enableassertions开启 SpringBoot 中提供了 Asser ...

  4. spring boot 文件上传工具类(bug 已修改)

    以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...

  5. 获取Spring容器管理的Bean工具类

    很多时候我们在一些不受spring管理的类中需要用到spring管理的Bean,那么这个时候可以使用如下工具类从spring容器中获取相关的Bean实例. @Component public clas ...

  6. 解决spring的读取文件的工具类来获取文件等操作

    今天项目又遇到了,,,读取项目下文件的情况下. 其实这个挺头疼的,,,特别是 Java web项目 下的方式和 Java 项目 下 读取文件的相对路径是有点区别的...所以感觉挺不好搞的... 而且 ...

  7. JMeter 控制并发数

    文章目录 一.误区 二.正确设置 JMeter 的并发数 总结 没用过 JMeter 的同学,可以先过一遍他的简单使用例子 https://blog.csdn.net/weixin_42132143/ ...

  8. 从spring容器中获取对象工具类

    工具类: public class SpringConfigTool implements ApplicationContextAware {private static ApplicationCon ...

  9. 记录一下spring静态获取bean的工具类

    /*** @author: hzc* @Date: 2019/07/30 14:43* @Description: 由于依赖spring容器只有spring容器初始化完成后才能使用 所以不能在任何的b ...

最新文章

  1. 从零开始单排学设计模式「UML类图」定级赛
  2. git commit -amend_Github标星3W+,这份Git飞行规则你值得拥有
  3. Django 1.10中更新modules同步数据库
  4. django中collectstatic的使用(亲测)
  5. HBase Replication源码解析之HLog读取
  6. 从今天起,开始等待中信世界杯信用卡
  7. LeetCode 464. 我能赢吗(状态压缩+记忆化递归 / 博弈)
  8. pandas分析各国家交易情况
  9. mysql的优化-添加环境变量启动服务
  10. 云服务器svn怎么用,腾讯云服务器搭建SVN
  11. mysql在官网下载完解压后安装
  12. 谈谈我对数据结构的理解
  13. Spring学习资料
  14. 8位可控加减法电路设计
  15. 黄一老师:征信问答这些信用知识你都了解吗?
  16. B站台湾大学郭彦甫|MATLAB 学习笔记|12 线性方程式和线性系统 Linear equations
  17. 【论文写作】汽车销售管理系统的设计与开发引言参考
  18. 自动驾驶(三十四)---------可行驶区域检测
  19. 快餐行业进入“黄金时代”,左手天堂右手地狱
  20. EBS HRMS数据表

热门文章

  1. 小而美的个人博客——前端——about
  2. scrapy的name变量_python-将file_name参数传递给管道以在scrapy中...
  3. android view clip,Android 自定义View Clip
  4. 成考高起专计算机统考试题,成考考试题型
  5. Cortex-M3的工作模式和特权级别
  6. vb.net限制datagridview不能选择_事业单位考试有哪些条件限制?
  7. 口袋操作系统_可以装进口袋的主机要有多小?驰为LarBox迷你主机入手体验
  8. debug疯了_作业没疯,我先疯了 益智解谜《作业疯了》试玩
  9. java软件测试方法有哪些方法有哪些_如何才算掌握Java
  10. pythonspark集群模式运行_有关python numpy pandas scipy 等 能在YARN集群上 运行PySpark