模拟并发请求的几种方式

1.使用Postman

操作步骤:

  1)进入一个collection,点击run按钮:

  2)选择需要并发请求的接口,配置数量以及延迟等,点击运行:

   3)查看运行结果:

 

2.代码使用CountDownLatch

了解CountDownLatch:https://blog.csdn.net/yangshangwei/article/details/121155013

import com.alibaba.fastjson.JSONObject;
import com.zyuan.boot.entity.EventOrder;
import com.zyuan.boot.mapper.EventOrderMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.CountDownLatch;

@SpringBootTest
@RunWith(SpringRunner.class)
public class CountDownLatchTest {

    @Autowired
    private EventOrderMapper eventOrderMapper;

    @Test
    public void selectById() {
        // 模拟5个并发请求
        CountDownLatch latch = new CountDownLatch(5);
        for (Integer i = 1; i <= 5; i++) {
            Long id = i.longValue();
            new Thread(() -> {
                try {
                    // 先阻塞改线程
                    latch.await();
                    EventOrder eventOrder = eventOrderMapper.selectById(id);
                    String eventStr = JSONObject.toJSONString(eventOrder);
                    System.out.println(eventStr);
                    System.out.println("===========");
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
            }).start();
            try {
                Thread.sleep(10);
                // 让计数器-1,直至为0,唤醒所有被CountDownLatch阻塞的线程
                latch.countDown();
                System.out.println("countDownLatch执行一次,剩余count:" + latch.getCount());
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
        }
    }

}

点击运行查看控制台日志:

 

posted @ 2022-09-16 17:55  LonZyuan  阅读(1009)  评论(0编辑  收藏  举报