JUC共享锁之Semaphore

Semaphore原意是指信号量,从API的注释:"Semaphores are often used to restrict the number of threads than can access some (physical or logical) resource"可以看出,Semaphore一般是用来限制线程能够使用的资源个数.

应用场景

在Web开发中,Semaphore可以用来限制某个接口的并发调用次数.可以在Sping的Context中维护一个Map,key可以是处理线程,value可以是一个Semaphore对象.通过这样的方式,就可以实现系统同时处理的线程数不会超过某个阈值.

代码实现

我们可以通过WebFilter注解,来实现一个过滤器,在过滤器中拦截所有的请求调用,然后通过Semaphore来进行计数,如果超过总的计数,则返回相应的提示信息.当然也可以对URL进行细化,针对每个API提供对应的限制.

/**
 * API并发控制过滤器
 * Created by qiaohe
 * Date: 19-7-1 上午11:54
 */
@Component
@WebFilter(urlPatterns = "/*", filterName = "concurrentRestrictFilter")
public class ConcurrentRestrictFilter implements Filter {
    private Log log = LogFactory.getLog(getClass());

    private static final Integer MAX_CONCURRENT_NUM = 1;

    private static final Semaphore semaphore = new Semaphore(MAX_CONCURRENT_NUM);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        log.info("before acquire: " + semaphore.availablePermits());
        if(!semaphore.tryAcquire()){
            if(response instanceof HttpServletResponse){
                HttpServletResponse res = (HttpServletResponse)response;
                res.setContentType(MimeTypeUtils.APPLICATION_JSON_VALUE);
                res.sendError(HttpStatus.BAD_REQUEST.value(), "reach max current num");
            }
            return;
        }
        log.info("after acquire: " + semaphore.availablePermits());
        try{
            chain.doFilter(request, response);
        } finally {
            semaphore.release();
            log.info("release: " + semaphore.availablePermits());
        }
    }

    @Override
    public void destroy() {

    }
}

转自: http://heqiao2010.github.io

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 【threading模块详解】 模块基本方法 该模块定了的方法如下:threading.active_count(...
    奕剑听雨阅读 1,062评论 0 0
  • 1.设计模式是什么? 你知道哪些设计模式,并简要叙述?设计模式是一种编码经验,就是用比较成熟的逻辑去处理某一种类型...
    龍飝阅读 2,180评论 0 12
  • "你还有日夜思念的人吗?" "没有" "但是,与你擦肩而过的每一个人都有可能是别人日夜思念的人啊!" "也包括我吗...
    扶玉楼阅读 212评论 0 0
  • 考神附体千阅读 42评论 0 0
  • 《 鲸鱼是楼下的海》这本书的作者生动形象的写了家乡青岛的建筑史,这是一种独具特色的乡土文学,对乡土文学,我的...
    包本啵啵阅读 535评论 0 2