官方文档:

/*** 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. 杭电2682--Tree(Prim)
  2. Dropout的前世与今生
  3. 使用refs获取节点_闲庭信步聊前端 - 原来你是这样的Refs
  4. 一起玩转CoordinatorLayout
  5. Algorithms - Insertion Sort - 插入排序
  6. buf.indexOf()
  7. python 中断线程_如何编写快速且线程安全的Python代码
  8. 求助批量修改kml文件内容
  9. Shiro面试题(二十道)
  10. 套路得人心之我的运营之路!(文末有福利)
  11. Eplan教程——项目检查错误 005013/005014:连接点类型不同
  12. 云计算关键概念之一:云资源池
  13. 《凤凰架构》读后感 - 演进中的架构
  14. python打开其他应用程序错误_Python应用程序错误(Udacity)
  15. android绑定交通卡,【NFC-SIM卡刷公交教程】支持安卓8.0(3月23日更新)
  16. 一年读了八十本书之后,我推荐这五本给你
  17. 考研计算机320分什么水平,考研320分算什么水平,能上211、985吗?很多人都答不上...
  18. 新增spring Converter解析器中使用lambda表达式代替匿名内部类是启动报错:... does the class parameterize those types?
  19. Flash鼠绘入门第四课:绘制漂亮的梅花-梅花照片分析与学习
  20. 我的世界红石计算机教程1,《我的世界》红石电脑制作原理及使用教程

热门文章

  1. java 变量单例_Java静态变量的用法:伪单例
  2. mysql mmm 主主_Microsoft Azure部署MYSQL-MMM(2)配置主主复制
  3. java软件测试技能要求_软件测试需要掌握什么技能
  4. idea启动webservice_Intellij Idea 之 WebService客户端测试
  5. 请求时的编码问题 Use body.encode(‘utf-8‘) if you want to send it encoded in UTF-8
  6. Android中的Fragment
  7. Qt中的QTimer
  8. 山东春考计算机专科学校排名,山东春考本科专科学校有哪些
  9. python怎么画多重饼状图_Python通过matplotlib画双层饼图及环形图简单示例
  10. 公安网安装mysql 5.7_安装Mysql 5.7.1