ConcurrentHashMap<String,List<K8sPod>> podListMap = new ConcurrentHashMap<>();
int length = nameSpaces.size();
ExecutorService pool = Executors.newFixedThreadPool(length);
CountDownLatch latch = new CountDownLatch(length);
for (String namespace :nameSpaces){
List<K8sPod> finalPodList = Collections.synchronizedList(new ArrayList<>());
Map<String, String> finalProjects = projects;
pool.submit(()-> {
try {
queryHealth( namespace.toLowerCase(), finalPodList,k8sPod.getName(), finalProjects ,k8sPod.getAge());
podListMap.put(namespace.toLowerCase(),finalPodList);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
latch.countDown();
}
});
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (String namespace :nameSpaces){
podList.addAll(podListMap.get(namespace));
}
CountDownLatch实现 并发查询
于 2022-03-04 14:23:43 首次发布