【发布时间】:2020-08-01 06:37:46
【问题描述】:
我正在调用多个rest API,为此我使用ExecutorService 进行并行处理。消费者每秒点击我的应用程序超过 10 次,我观察到 ExecutorService 响应非常缓慢。我已经使用 tomcat Web 服务器在kubernetes 上部署了我的应用程序。
下面是我的代码,不知道是什么原因导致它变慢。
ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(100);
Collection tasks = new ArrayList();
for( String str: datalist){
tasks.add(new MyThreadPool(str)); // MyThreadPool internally calls the REST API using Google HttpRequest.
}
List<Future<BasePolicy>> futures = null;
try {
long startProcessingTime = System.currentTimeMillis();
futures = WORKER_THREAD_POOL.invokeAll(tasks);
WORKER_THREAD_POOL.shutdown();
if (!WORKER_THREAD_POOL.awaitTermination(60000, TimeUnit.SECONDS)) {
WORKER_THREAD_POOL.shutdownNow();
}
long totalProcessingTime = System.currentTimeMillis() - startProcessingTime;
log.info("total time to complete the thread pool- " + totalProcessingTime);
} catch (InterruptedException e) {
log.error("error occured during ASYNC process ");
e.printStackTrace();
}
log.info("Finished Waiting All threads completed ");
for (Future<Data> mFuture : futures) {
// my logic
}
【问题讨论】:
-
我相信可能是外部服务没有快速响应。您是否检查了从外部服务获得响应需要多长时间?。
-
@harry,我的后端休息服务响应速度很快,我已经设置并验证了计时器。如果我一次收到一个请求,它只有在多个请求到达我的服务时才有效,那么我会看到缓慢和挂起的问题
标签: java multithreading kubernetes executorservice threadpoolexecutor