Executor Service 在多线程环境下清除映射值

Executor Service clear map values in multithreading environment

我这里有个情况。我正在使用 Executor Service 来利用多线程。所以现在我正在尝试向地图添加一个值,并在完成该循环后立即为每个线程清除它。我为此编写了以下代码。

我创建了一个地图,并保存了每次迭代的值并将其清除。但是当我使用 Executor Service 它创建 10thread 并将值添加到我相信的地图时。这就是为什么我能够看到多个值,即使我只是添加一个值并清除它。

那么我如何清除循环中每个交易的地图:(

代码:

public class Test1 {
public static void main(String[] args){
    ExecutorService executor = Executors.newFixedThreadPool(10);
    final Multimap<Object, Object> map = Multimaps.synchronizedMultimap(ArrayListMultimap.create());

    final List<String> listA = new ArrayList<String>();
    listA.add("e");
    listA.add("f");
    listA.add("g");
    final List<String> listB = new ArrayList<String>();
    listB.add("e");
    listB.add("f");
    listB.add("g");
    final List<String> listC = new ArrayList<String>();
    listC.add("e");
    listC.add("f");
    listC.add("g");


    for (final String stateId : listA) {
        for (final String gradeList : listB) {
            for (final String subjectList : listC) {
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                map.clear();
                map.put("f", "hi");
                System.out.println("map before "+map);

                System.out.println("map after "+map);
            }
        });
    }

    }
    }
    executor.shutdown();
    try {
        executor.awaitTermination(24L, TimeUnit.HOURS);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

输出:

map before {f=[hi, hi]}
map before {f=[hi]}
map before {f=[hi]}
map before {f=[hi]}
map before {f=[hi]}
map before {f=[hi]}
map before {f=[hi]}
 map before {f=[hi]}
map after {f=[hi]}
map after {f=[hi]}
map after {f=[hi]}
map before {f=[hi, hi]}
map before {f=[hi, hi]}
map after {f=[hi]}
map after {f=[hi]}
map before {f=[hi]}
map after {f=[hi]}
map before {f=[hi]}
map after {f=[hi]}
map before {f=[hi]}
 map after {f=[hi]}
map after {f=[hi]}
map before {f=[hi]}
map before {f=[hi]}
map before {f=[hi]}
map after {f=[hi]}
map before {f=[hi]}
map after {f=[hi]}
map after {f=[hi]}
map before {f=[hi]}
map before {f=[hi]}
map after {f=[hi]}
map after {f=[hi, hi]}
map after {f=[hi]}
map before {f=[hi]}
map before {f=[hi]}
map before {f=[hi]}
map after {f=[hi]}
 map after {f=[hi]}
map before {f=[hi]}
map before {f=[hi]}
map after {f=[hi]}
map after {f=[hi]}
map before {f=[hi]}
map after {f=[hi]}
 map after {f=[hi]}
map after {f=[hi]}
map after {f=[hi]}
map before {f=[hi]}
map before {f=[hi]}
map after {f=[hi]}
map after {f=[hi]}
map after {f=[hi]}
map after {f=[hi]}

简单 - javadoc 明确指出:

This class is not threadsafe when any concurrent operations update the multimap. Concurrent read operations will work correctly. To allow concurrent update operations, wrap your multimap with a call to Multimaps.synchronizedListMultimap(com.google.common.collect.ListMultimap).

换句话说:您不能只使用任何方便的集合 class 并向其抛出多个线程 - 没有进一步的预防措施。