首页 > 其他分享 >SpringBoot 接口并发限制(Semaphore)

SpringBoot 接口并发限制(Semaphore)

时间:2022-12-06 11:36:16浏览次数:65  
标签:11 http nio 14 exec 并发 6201 Semaphore SpringBoot

可以使用 JMeter 辅助测试 https://blog.csdn.net/weixin_45014379/article/details/124190381

@RestController
@RequestMapping({"/Test"})
public class test { 

    Logger logger = LoggerFactory.getLogger(this.getClass());
 
    // 使用 Semaphore 并发限制3个 超过阻塞
    private final Semaphore permit = new Semaphore(3, true);


    @GetMapping(value = {"/download"})
    public String download(HttpServletRequest request) {        
        String result = "";
        try {
            // 获取令牌			
            logger.info("锁住 {}", Thread.currentThread().getName());
            permit.acquire();
            logger.info("下载 {}", Thread.currentThread().getName());
            Thread.sleep(5000);
            result = "Success";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        } finally {
            // 释放令牌
            permit.release(); 
            logger.info("释放 {}", Thread.currentThread().getName());
        } 
    }

}
[2022-11-29 14:33:05.042] [http-nio-6201-exec-1] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-1
[2022-11-29 14:33:05.042] [http-nio-6201-exec-1] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-1
[2022-11-29 14:33:05.097] [http-nio-6201-exec-4] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-4
[2022-11-29 14:33:05.097] [http-nio-6201-exec-4] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-4
[2022-11-29 14:33:05.156] [http-nio-6201-exec-3] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-3
[2022-11-29 14:33:05.156] [http-nio-6201-exec-3] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-3
[2022-11-29 14:33:05.204] [http-nio-6201-exec-5] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-5
[2022-11-29 14:33:05.205] [http-nio-6201-exec-5] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-5
[2022-11-29 14:33:05.241] [http-nio-6201-exec-2] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-2
[2022-11-29 14:33:05.241] [http-nio-6201-exec-2] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-2
[2022-11-29 14:33:05.291] [http-nio-6201-exec-6] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-6
[2022-11-29 14:33:05.292] [http-nio-6201-exec-6] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-6
[2022-11-29 14:33:05.341] [http-nio-6201-exec-7] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-7
[2022-11-29 14:33:05.341] [http-nio-6201-exec-7] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-7
[2022-11-29 14:33:05.396] [http-nio-6201-exec-8] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-8
[2022-11-29 14:33:05.396] [http-nio-6201-exec-8] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-8
[2022-11-29 14:33:05.442] [http-nio-6201-exec-9] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-9
[2022-11-29 14:33:05.442] [http-nio-6201-exec-9] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-9
[2022-11-29 14:33:05.492] [http-nio-6201-exec-10] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-10
[2022-11-29 14:33:05.492] [http-nio-6201-exec-10] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-10
[2022-11-29 14:33:05.542] [http-nio-6201-exec-11] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-11
[2022-11-29 14:33:05.592] [http-nio-6201-exec-13] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-13
[2022-11-29 14:33:05.642] [http-nio-6201-exec-12] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-12
[2022-11-29 14:33:05.691] [http-nio-6201-exec-14] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-14
[2022-11-29 14:33:05.742] [http-nio-6201-exec-16] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-16
[2022-11-29 14:33:05.792] [http-nio-6201-exec-15] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-15
[2022-11-29 14:33:05.842] [http-nio-6201-exec-17] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-17
[2022-11-29 14:33:05.892] [http-nio-6201-exec-18] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-18
[2022-11-29 14:33:05.942] [http-nio-6201-exec-19] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-19
[2022-11-29 14:33:05.992] [http-nio-6201-exec-20] [INFO ] com.vipsoft.controller.ReleaseController - 锁住 http-nio-6201-exec-20
[2022-11-29 14:33:08.045] [http-nio-6201-exec-1] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-1
[2022-11-29 14:33:08.045] [http-nio-6201-exec-11] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-11
[2022-11-29 14:33:08.105] [http-nio-6201-exec-4] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-4
[2022-11-29 14:33:08.105] [http-nio-6201-exec-13] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-13
[2022-11-29 14:33:08.165] [http-nio-6201-exec-3] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-3
[2022-11-29 14:33:08.165] [http-nio-6201-exec-12] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-12
[2022-11-29 14:33:08.210] [http-nio-6201-exec-5] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-5
[2022-11-29 14:33:08.210] [http-nio-6201-exec-14] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-14
[2022-11-29 14:33:08.256] [http-nio-6201-exec-2] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-2
[2022-11-29 14:33:08.256] [http-nio-6201-exec-16] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-16
[2022-11-29 14:33:08.302] [http-nio-6201-exec-6] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-6
[2022-11-29 14:33:08.302] [http-nio-6201-exec-15] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-15
[2022-11-29 14:33:08.347] [http-nio-6201-exec-7] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-7
[2022-11-29 14:33:08.347] [http-nio-6201-exec-17] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-17
[2022-11-29 14:33:08.408] [http-nio-6201-exec-8] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-8
[2022-11-29 14:33:08.408] [http-nio-6201-exec-18] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-18
[2022-11-29 14:33:08.454] [http-nio-6201-exec-19] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-19
[2022-11-29 14:33:08.454] [http-nio-6201-exec-9] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-9
[2022-11-29 14:33:08.498] [http-nio-6201-exec-10] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-10
[2022-11-29 14:33:08.498] [http-nio-6201-exec-20] [INFO ] com.vipsoft.controller.ReleaseController - 工作 http-nio-6201-exec-20
[2022-11-29 14:33:11.047] [http-nio-6201-exec-11] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-11
[2022-11-29 14:33:11.109] [http-nio-6201-exec-13] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-13
[2022-11-29 14:33:11.167] [http-nio-6201-exec-12] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-12
[2022-11-29 14:33:11.213] [http-nio-6201-exec-14] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-14
[2022-11-29 14:33:11.258] [http-nio-6201-exec-16] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-16
[2022-11-29 14:33:11.304] [http-nio-6201-exec-15] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-15
[2022-11-29 14:33:11.350] [http-nio-6201-exec-17] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-17
[2022-11-29 14:33:11.411] [http-nio-6201-exec-18] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-18
[2022-11-29 14:33:11.458] [http-nio-6201-exec-19] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-19
[2022-11-29 14:33:11.503] [http-nio-6201-exec-20] [INFO ] com.vipsoft.controller.ReleaseController - 释放 http-nio-6201-exec-20
Disconnected from the target VM, address: '127.0.0.1:7367', transport: 'socket'

Process finished with exit code -1

标签:11,http,nio,14,exec,并发,6201,Semaphore,SpringBoot
From: https://www.cnblogs.com/vipsoft/p/16935411.html

相关文章

  • 对graalvm、springboot3.0一些新特性的探究
    环境:系统:IntelcoreMacVentura13.0.1工具: Idea:2022.2.3 gradle:7.4(idea自带的)  openjdk:version"17.0.5"2022-10-18 graalvm: CE22.3.0 ......
  • [C++11与并发编程]读写锁的简单实现
    读写锁的简单实现layout:posttitle:读写锁categories:cpp_concurrencydescription:C++并发编程简介keywords:c++,并发编程,读写锁​​boost共享锁实现​​​读写......
  • 使用 aop + guava 为 springboot 应用做限流
    1.引入guava依赖<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId></dependency>编写......
  • 深刻理解JAVA并发中的有序性问题和解决之道
    问题Java并发情况下总是会遇到各种意向不到的问题,比如下面的代码:intnum=0;booleanready=false;//线程1执行此方法publicvoidactor1(I_Resultr){if(rea......
  • SpringBoot防止XSS
    前言:什么是XSS在跨站脚本(XSS)攻击中,攻击者可以在受害者的浏览器中执行恶意脚本。这种攻击通常是通过在网页中插入恶意代码(JavaScript)来完成的。攻击者在使用攻击后一般......
  • Springboot 系列 (20) - Springboot+ElasticSearch 实现全文搜索(二)
    在“Springboot系列(19)-Springboot+ElasticSearch实现全文搜索(一)”里我们演示了安装配置ElasticSearch和使用curl调用ElasticSearchAPI,本文将演示在Springb......
  • SpringBoot整合Spring Data JPA多数据源
    Springboot整合springdatajpa多数据源在使用Mybatis时会涉及多数据源的问题,同样,当我们使用Jpa作为持久化处理方式时,也会涉及多数据源的问题,本节我们来讲解一下Springboot......
  • Springboot 系列 (19) - Springboot+ElasticSearch 实现全文搜索(一)
    全文检索(Full-textSearch)是指计算机索引程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索......
  • 第十一章-并发控制
    11章并发数据了解并发需要先了解的概念:1.数据库是一个可以共享资源,可以多用户同时使用一个数据库,该数据库称为多用户数据库,如订票系统、银行系统2.多事务执行方式:......
  • JUC并发编程
    什么是JUCJDK1.5出现的,用来处理线程的工具包进程与线程进程:指在系统中正在运行的一个应用程序;程序一旦运行就是进程;进程一-资源分配的最小单位。线程:系统分配处理器时......