日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring控制并发数的工具类ConcurrencyThrottleSupport和ConcurrencyThrottleInterceptor

發布時間:2025/4/5 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring控制并发数的工具类ConcurrencyThrottleSupport和ConcurrencyThrottleInterceptor 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

官方文檔:

/*** 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。