java高并发测试实例(精确到几百纳秒)

其实就是用CountDownLatch来使多个线程同时运行,同时发送请求,虽然post的事件不能保证同时,但发post能保证同时(相差0.2毫秒),通过这种方法,测试了redis的setkey,getkey来保证不重复数据的时间阈值在几百纳秒,再短就算redis也无能为力了,所以多线程虽然快,但是没锁,还是不太安全。。

@Service

public class TestConcurrentService {


    @Autowired
    private ApiHttpClientService apiHttpClientService;


    private static final Logger logger = Logger.getLogger(TestConcurrentService.class);


    public void testConcurrent1(String sign) {


        ExecutorService exec = Executors.newFixedThreadPool(3);
        CountDownLatch begin = new CountDownLatch(1);
        CountDownLatch end = new CountDownLatch(3);
        for (int i = 0; i < 3; i++) {
            exec.execute(new HttpRequestUtils(begin, end, sign));


        }
        begin.countDown();


        try {
            end.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
        }
        exec.shutdown();
        logger.info("main method end");
    }


    public class HttpRequestUtils implements Runnable {
        private Logger log = Logger.getLogger(HttpRequestUtils.class);


        private CountDownLatch begin;
        private CountDownLatch end;


        private String sign;


        private String url = "http://localhost:8080/score/private/v1/injectsg";


        public HttpRequestUtils(CountDownLatch begin, CountDownLatch end, String sign) {
            this.begin = begin;
            this.end = end;
            this.sign = sign;


        }


        @Override
        public void run() {
            try {
                begin.await();
                Map<String, String> header = new HashMap<>();
                header.put("Content-Type", "application/json");
                header.put("sign", sign);


                ScoreRequestBean bean = new ScoreRequestBean();
                Data data = new Data();
                data.setRemark("单发测试");
                data.setExtId("ext111111519");
                data.setPartnerMemberId(297);
                data.setPoints(1);
                bean.setData(data);
                bean.setEventId("123asdasd");
                System.out.println("线程" + Thread.currentThread().getName() +
                        "已接受命令," + "时间为:" + System.nanoTime() + "|" + System.nanoTime() / 1000000L);


                apiHttpClientService.post(url, null, header, bean, ResultConstant.class);
                System.out.println("线程" + Thread.currentThread().getName() +
                        "命令已结束");
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            } finally {
                end.countDown();
            }


        }
    }




}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值