通過CountDownLatch模擬併發請求

package com.hrfax;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

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

    private static int count = 100;
    private static CountDownLatch countDownLatch = new CountDownLatch(count);

    @Test
    public void test1() throws InterruptedException{
        for (int i=0;i<count;i++){
            MyRunnable myRunnable = new MyRunnable();
            Thread thread = new Thread(myRunnable);
            thread.start();
            countDownLatch.countDown();
        }
        Thread.currentThread().join();
    }

    public class MyRunnable implements Runnable{

        @Override
        public void run() {
            try {
                countDownLatch.await();
            }catch (Exception e){

            }
            // 發送Http請求
            RestTemplate restTemplate = new RestTemplate();
            List<HttpMessageConverter<?>> list = new ArrayList<>();
            list.add(new FastJsonHttpMessageConverter());
            restTemplate.setMessageConverters(list);
            // restTemplate.getForObject("","");
        }
    }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章