如何设置线程数以及CountDownLatch数,才能保证多线程是同时跑了,而不丢数据?目前设置1w个线程,发现返回s数据丢失几个

private static final int THREAD_NUM = 10000;
//并发 压测工具
private CountDownLatch countDownLatch = new CountDownLatch(10000);

@Test
public void testInterface() throws Exception{
    for (int i = 0; i < THREAD_NUM; i++) { //模拟多线程
final  long orderId =  i;
          System.out.println(i);
          Thread thread = new Thread( ()->{
              countDownLatch.countDown();
              try {
                  countDownLatch.await();
                  String s = HttpUtils.sendGet("http://localhost:30111/demo/queryMovie?movieCode="+orderId);
                  //请问业务接口
System.out.println(Thread.currentThread().getName() + orderId + "========orderId==============="+s);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          });
        thread.start();
    }
}

回答

你这个await写法本身就有问题 这个是等待所有结束得逻辑 只用执行一次得 你把这个放到for循环后面就OK了