在多线程环境中清除嵌套循环中的映射

Clear a Map in nested loop in multithreaded environement

我在多线程环境中工作,我有一个同步多图。我在嵌套循环中使用该映射,我想清除该嵌套循环中每个事务的值。我怎样才能做到这一点:

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();
        }
    }

但这并没有导致正确的输出。有时我可以看到多个值,有时我在打印地图时看不到任何值。有人可以为此提出更改建议吗?

你在执行器中同时运行所有操作(好吧,一次 10 个)。这些调用在这里:

        map.clear();
        map.put("f", "hi");
        System.out.println("map before "+map);
        System.out.println("map after "+map);

不是 同步块的一部分,来自多个线程的指令可能会在它们之间交错,f.e。以下是跨线程 T1 和 T2 的有效交错:

 T1:       map.clear();
 T2:       map.clear();
 T1:       map.put("f", "hi");
 T2:       map.put("f", "hi");
 T2:       System.out.println("map before "+map);
 T1:       System.out.println("map before "+map);
 T1:       System.out.println("map after "+map);
 T2:       System.out.println("map after "+map);

我将把如何跨多个指令同步作为 reader 的练习。