如何避免这种繁忙的循环?

时间:2019-04-27 14:01:54

标签: java concurrency

我正在尝试优化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
    }

有一个忙循环,等待服务终止。我该如何避免呢?

0 个答案:

没有答案