Java 多线程环境中的执行器服务清除映射值

Java 多线程环境中的执行器服务清除映射值,java,multithreading,executorservice,Java,Multithreading,Executorservice,我这里有个情况。我使用Executor服务来利用多线程。因此,现在我尝试向映射添加一个值,并在完成该循环后立即为每个线程清除该值。我已经为此编写了以下代码 我已经创建了一个映射,并保存每次迭代的值并清除它。但当我使用Executor服务时,它创建了10个线程并为地图添加了价值,我相信。这就是为什么我能够看到多个值,即使我只是添加一个值并清除它 那么,如何清除循环中每个传输的映射:( 代码: 简单-协议明确规定: 当任何并发操作更新multimap时,此类不是线程安全的。并发读取操作将正常工作。若

我这里有个情况。我使用Executor服务来利用多线程。因此,现在我尝试向映射添加一个值,并在完成该循环后立即为每个线程清除该值。我已经为此编写了以下代码

我已经创建了一个映射,并保存每次迭代的值并清除它。但当我使用Executor服务时,它创建了10个线程并为地图添加了价值,我相信。这就是为什么我能够看到多个值,即使我只是添加一个值并清除它

那么,如何清除循环中每个传输的映射:(

代码:

简单-协议明确规定:

当任何并发操作更新multimap时,此类不是线程安全的。并发读取操作将正常工作。若要允许并发更新操作,请通过调用Multimaps.synchronizedListMultimap(com.google.common.collect.ListMultimap)包装您的multimap


换句话说:你不能在没有进一步预防措施的情况下使用任何方便的收集类并向其抛出多个线程。

你的期望是什么?它的行为应该是这样的。但我想在循环结束时清除映射。这样我就可以在循环中为新的迭代保存新值并继续。有解决方案吗?@Jerry06能否请您提供一个示例,说明如何在上述场景中实现此功能。这将是一个很大的帮助。我刚刚学习了多线程,并且了解了javadoc的含义。但我并不确定如何实现它。谷歌搜索结果也不多。因此,您能否至少提供一个示例?@GhostCati尝试了这个方法:final Multimap map=Multimaps.synchronizedMultimap(ArrayListMultimap.create());现在我很少看到地图包含两个值。我在问题中也更新了这一点。请更正我?请避免陷入“更多问题”当这个问题得到充分的回答时,你应该考虑为这个新问题打开一个新的问题。总之,我今天就要结束了。所以如果其他人有一个“更完整的”答案,请自由地接受这个答案。否则,请明天给我一个评论,然后我再看一看你的问题。
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]}