java模拟实现真正的同时并发请求

有时需要测试一下某个功能的并发性能,又不要想借助于其他工具,索性就自己的开发语言,来一个并发请求就最方便了。

java中模拟并发请求,自然是很方便的,只要多开几个线程,发起请求就好了。但是,这种请求,一般会存在启动的先后顺序了,算不得真正的同时并发!怎么样才能做到真正的同时并发呢?是本文想说的点,java中提供了闭锁 CountDownLatch, 刚好就用来做这种事就最合适了。

只需要:

  1. 开启n个线程,加一个闭锁,开启所有线程;

  2. 所有线程都准备好后,按下开启按钮,就可以真正的发起并发请求了。

    package com.zx.demo.countDownLatch;
    import com.zx.demo.util.JedisClusterUtil;
    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    import javax.annotation.Resource;
    import java.util.concurrent.CountDownLatch;
    
    @SpringBootTest
    public class CountDownLacthTest {
    
        @Test
        public void testRedisLock() throws InterruptedException {
            //同时并发5个请求
            startTaskAllInOnce(5);
        }
    
        public long startTaskAllInOnce(int threadNums) throws InterruptedException {
            //一个开启开关(一次开启,多个线程一起开始)
            final CountDownLatch startGate = new CountDownLatch(1);
            //一个关闭开关(多次结束,多个线程分别结束)
            final CountDownLatch endGate = new CountDownLatch(threadNums);
            for(int i = 0; i < threadNums; i++) {
                Thread t = new Thread(() -> {
                    try {
                        //开启开关未打开,所有new子线程的执行在此处阻塞,等待开启开关打开时,一起并发执行
                        startGate.await();
                        try {
                            //调起线程
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    System.out.println("子线程名:"+Thread.currentThread().getName()+"开始执行"+System.currentTimeMillis());
                                    //下面写要并发的接口请求即可
                                }
                            }).run();
                        } finally {
                            //当前子线程执行结束,将关闭开关线程数减一,减到零时,关闭开关打开
                            endGate.countDown();
                        }
                    } catch (InterruptedException ie) {
                        ie.printStackTrace();
                    }
                });
                t.start();
            }
            //记录多线程开始执行时间
            long startTime = System.nanoTime();
            System.out.println(startTime + " [" + Thread.currentThread().getName() + "] All thread is ready, concurrent going...");
            //主线程将开启开关线程数减一,因初始值为1,直接减到零了,开启开关打开,上面阻塞的各子线程并发执行
            startGate.countDown();
            //关闭开关未打开,使当前主线程阻塞在此,等待上方多个子线程执行,当所有子线程执行完毕,关闭开关线程数减到零,关闭开关打开,继续执行主线程
            endGate.await();
            //记录多线程执行结束时间
            long endTime = System.nanoTime();
            System.out.println(endTime + " [" + Thread.currentThread().getName() + "] All thread is completed.");
            return endTime - startTime;
        }
    }
    

     执行效果

 

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您可以使用 Java 中的 HttpURLConnection 类来模拟发送 POST 请求。下面是一个示例代码: ```java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class PostmanSimulation { public static void main(String[] args) throws Exception { String url = "http://localhost:8080/api/createUser"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 请求方法设为 POST con.setRequestMethod("POST"); // 添加请求头 con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // 发送 POST 请求 con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes("username=test&password=123456"); wr.flush(); wr.close(); // 获取响应码和响应结果 int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + "username=test&password=123456"); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 打印响应结果 System.out.println(response.toString()); } } ``` 在这个示例代码中,我们首先设定了要发送 POST 请求的 URL 地址(这里是 localhost 上的一个测试接口),然后设置了一些请求头属性。紧接着,我们使用 DataOutputStream 将请求参数写入输出流中,并发送给服务器。接下来是获取响应码和响应结果的过程,最后输出了响应结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值