ConcurrentHashMap在多线程应用程序中没有给出正确的输出

时间:2014-01-06 11:10:13

标签: java concurrency

我创建了一个例子来理解并发HashMap,但它似乎在7-8次运行后给出了不确定的outout。

在我的例子中,我创建了三个线程(它们模仿三个服务来获取数学,科学,英语的分数),它更新了一个只有3个键值对的密码HashMap(键是三个名称A,B,C和运行结束时的值应为三个受试者的累积分数)

我发布下面的代码,请指教。

错误的输出显示如下(正确的一个应该像所有完成主要结束:{C = 27,A = 57,B = 42})

Within run Math : {C=0, A=0, B=0}
Within run Science : {C=0, A=0, B=0}
Completed Science : {C=10, A=39, B=29}
Completed Math : {C=10, A=39, B=29}
Within run English : {C=0, A=0, B=0}
Completed English : {C=18, A=57, B=42}
All Done main : {C=18, A=57, B=42}

ConcurrentHashMapExample类:

    package com.ll.thread.concurrency;

    import java.util.Iterator;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.CountDownLatch;

    public class ConcurrentHashMapExample {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ConcurrentHashMap<String, Integer>   concurrentHashMap = new ConcurrentHashMap<String, Integer>();
            concurrentHashMap.put("A", 0);
            concurrentHashMap.put("B", 0);
            concurrentHashMap.put("C", 0);
            CountDownLatch  countDownLatch = new CountDownLatch(3);

            Runnable runnableScience = new Worker(concurrentHashMap , "Science" , countDownLatch);
            Runnable runnableMath = new Worker(concurrentHashMap , "Math" , countDownLatch);
            Runnable runnableEnglish = new Worker(concurrentHashMap , "English" , countDownLatch);

            new Thread(runnableScience , "Science").start();
            new Thread(runnableMath ,"Math").start();
            new Thread(runnableEnglish ,"English").start();

            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("All Done " + Thread.currentThread().getName() + " : "+concurrentHashMap);




            concurrentHashMap = null;



        }

    }

工人阶级:

    package com.ll.thread.concurrency;

    import java.util.Iterator;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.CountDownLatch;

    public class Worker  implements Runnable{

        ConcurrentHashMap<String, Integer>  concurrentHashMap ;
        String threadName = "";
        CountDownLatch  countDownLatch ;

        public Worker(ConcurrentHashMap<String, Integer> concurrentHashMap,
                String subject, CountDownLatch countDownLatch) {
            this.concurrentHashMap = concurrentHashMap;
            this.threadName = subject ;
            this.countDownLatch = countDownLatch;

            // TODO Auto-generated constructor stub
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            System.out.println("Within run " +Thread.currentThread().getName() + " : "+concurrentHashMap);
            try{
         for(Iterator<String>  iterator = concurrentHashMap.keySet().iterator() ;   iterator.hasNext();){
            String key = iterator.next();

            //synchronized (this) {


            if("Math".equals(Thread.currentThread().getName())) {
            if("A".equals(key))
                concurrentHashMap.put(key, concurrentHashMap.get(key) +20);
            else if("B".equals(key))
                concurrentHashMap.put(key, concurrentHashMap.get(key) + 15);
            else if("C".equals(key))
                concurrentHashMap.put(key, concurrentHashMap.get(key) +10);
            }else

            if("Science".equals(Thread.currentThread().getName())) {
                if("A".equals(key))
                    concurrentHashMap.put(key, concurrentHashMap.get(key) +19);
                else if("B".equals(key))
                    concurrentHashMap.put(key, concurrentHashMap.get(key) +14);
                else if("C".equals(key))
                    concurrentHashMap.put(key, concurrentHashMap.get(key) +9);
                }
            else
                if("English".equals(Thread.currentThread().getName())) {
                    if("A".equals(key))
                        concurrentHashMap.put(key, concurrentHashMap.get(key) +18);
                    else if("B".equals(key))
                        concurrentHashMap.put(key, concurrentHashMap.get(key) +13);
                    else if("C".equals(key))
                        concurrentHashMap.put(key, concurrentHashMap.get(key) +8);
                    }

            }
        // }
            }
            finally{

                System.out.println("Completed " + Thread.currentThread().getName() + " : " + concurrentHashMap);
                countDownLatch.countDown();
            }
         }
        }

1 个答案:

答案 0 :(得分:1)

我认为您的测试中不需要ConcurrentHashMap。在执行线程之前初始化映射。相反,您可以使用常规HashMap参数化String键和AtomicInteger值:

HashMap<String, AtomicInteger> concurrentHashMap = ...
concurrentHashMap.put("A", new AtomicInteger(0));
...

然后你可以原子地增加每个线程中AtomicInteger实例的值:

concurrentHashMap.get(key).addAndGet(20);