官方文档:

/**
* 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 ConcurrencyThrottleSupport
implements MethodInterceptor, Serializable { public ConcurrencyThrottleInterceptor() {
setConcurrencyLimit(1);
} @Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
beforeAccess();
try {
return methodInvocation.proceed();
}
finally {
afterAccess();
}
} }

最新文章

  1. Centos6下安装高版本Git
  2. Apache Spark源码走读之16 -- spark repl实现详解
  3. java BufferedWriter and BufferedReader
  4. Selenium2学习-015-WebUI自动化实战实例-013-通过 URL 关闭多余的已开浏览器窗口
  5. SQL Server 2008 R2 找不到 Install SQL Server Profiler 找不到 事件探查器 解决
  6. linux mail命令用法
  7. Swift补基础之Selector、条件编译、编译标记、NSObject
  8. Data Mining 概念
  9. jQuery ajax error函数(交互错误信息的获取)
  10. Opengl4.5 中文手册—D
  11. Javascript写的一个可拖拽排序的列表
  12. ASP.NET MVC中URL末尾斜杠的实现
  13. css div 细边框
  14. Matlab绘图基础——axis设置坐标轴取值范围
  15. Django 基于session认证 小作业
  16. BlackArch安装(译文)
  17. DDD领域驱动设计理论篇 - 学习笔记
  18. Java学习笔记35(sql补充)
  19. Ubuntu下安装程序的三种方法(转)
  20. JavaScript基础一

热门文章

  1. 单页web应用是什么?它又会给传统网站带来哪些好处?
  2. python 之 Django 小案例
  3. java反射技术详解
  4. 工作中那些提高你效率的神器(第一篇)_Everything
  5. 为什么commonjs不适合于浏览器端
  6. centos 7.2 安装PHP7.1+apache2.4.23
  7. jquery ui 中的插件开发
  8. JS 判断字串字节数,并截取长度
  9. Session 、Application 和 HttpContext 的使用区别
  10. FMX保存JPG格式的Stream