我正在尝试优化Redis中的扫描操作。为此,我在每个节点上产生一个线程,并在每个节点上执行扫描操作。
这是代码
private Map<String, String> clusterScan(JedisCluster jc)
{
Map<String, String> valuesMap = new ConcurrentHashMap<>();
ExecutorService service = Executors.newFixedThreadPool(jc.getClusterNodes().size());
for (Map.Entry<String, JedisPool> nodeEntry : jc.getClusterNodes().entrySet())
{
service.submit(build(nodeEntry.getKey(), nodeEntry.getValue(), valuesMap));
}
service.shutdown();
while(!service.isTerminated()) {}
return valuesMap();
}
private Runnable build(String nodeName, JedisPool nodePool, Map<String, String> valuesMap)
{
// Runnable to update the Map per node
}
有一个忙循环,等待服务终止。我该如何避免呢?