增加线程数的高延迟

时间:2016-07-23 17:20:45

标签: java multithreading apache-httpclient-4.x countdownlatch

在下面的代码中DataGather = endDataGather - beginDataGather需要1.7ms &安培;服务响应的时间= service_COMPLETED - service_REQUEST_SENT 从20us到200 us不等(因为它们在相同的局域网上被模拟为虚拟因此如此之低) 现在,如果我将tomcat8线程从10增加到200,DataGather增加到150ms +,即使我将线程从200增加到1000,它甚至增加250 +。机器规格8核氙,64gb内存。当apache基准测试运行-n 40000 -c 100 args时,会测量时间,这是由于线程调度/上下文切换还是其他原因?我如何摆脱这种变化?当实际服务出现在20-100毫秒的延迟时,它是否仍然存在。

       public List<ServiceResponse> getData(final List<Service> services, final Data data) {
          //beginDateGather;

          final List<ServiceResponse> serviceResponses = Collections.synchronizedList(new ArrayList<>());
          try {
            final CountDownLatch latch = new CountDownLatch(services.size());
            Map<Future<HttpResponse>, HttpRequestBase> responseRequestMap = new HashMap<Future<HttpResponse>, HttpRequestBase>();

            for (final service service : services) {
              //creating request for a service
              try {
                HttpRequestBase request = RequestCreator.getRequestBase(service, data);
                //service_REQUEST_SENT
                Future<HttpResponse> response = client.execute(request,
                    new MyFutureCallback(service, data, latch, serviceResponses));
                responseRequestMap.put(response, request);
              } catch (Exception e) {
                latch.countDown();
              }
            }
            try {
              boolean isWaitIsOver = latch.await(timeout, TimeUnit.MILLISECONDS);
              if (!isWaitIsOver) {
                for (Future<HttpResponse> response : responseRequestMap.keySet()) {
                  if (!response.isDone()) {
                    response.cancel(true);
                  }
                }
              }
            } catch (InterruptedException e) {
            }
          } catch (Exception e) {
          }
          //endDataGather
          return serviceResponses;
    }


     public class MyFutureCallback implements FutureCallback<HttpResponse> {

        private Service service;
        private Data data;
        private CountDownLatch latch;
        private List<serviceResponse> serviceResponses;

        public MyFutureCallback( Service service, Data data, CountDownLatch latch, List<ServiceResponse> serviceResponses) {
          this.service = service;
          this.data = data;
          this.latch = latch;
          this.serviceResponses = serviceResponses;
        }

        @Override
        public void completed(HttpResponse result) {
          try {
            ServiceResponse serviceResponse = parseResponse(result, data, service);
              serviceResponses.add(serviceResponse);
          } catch (Exception e) {
          } finally {
            //service_COMPLETED
            latch.countDown();
          }
        }

        @Override
        public void failed(Exception ex) {
          latch.countDown();
        }

        @Override
        public void cancelled() {
          latch.countDown();
        }
       }

1 个答案:

答案 0 :(得分:1)

是的,似乎是由于线程的上下文切换。 在这种情况下,增加线程数将不会有所帮助。 您可以使用线程池进行回调。 请查看此链接以供参考,并尝试使用.PoolingClientAsyncConnectionManager

How to use HttpAsyncClient with multithreaded operation?