gpt4 book ai didi

java - ConcurrentHashMap 在多线程应用程序中未给出正确的输出

转载 作者:太空宇宙 更新时间:2023-11-04 07:08:43 24 4
gpt4 key购买 nike

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

在我的示例中,我创建了三个线程(它们模仿三个服务来获取数学、科学、英语的分数),它们更新一个只有 3 个键值对的并发 HashMap(键是三个名称 A、B、C,运行结束时的值应该是三个科目的累积分数)

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

错误的输出如下所示(正确的输出应该像 All Done main 一样结束:{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;



}

}

worker 类(Class):

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

最佳答案

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

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

然后您可以自动增加每个线程中 AtomicInteger 实例的值:

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

关于java - ConcurrentHashMap 在多线程应用程序中未给出正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20948756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com