gpt4 book ai didi

java - 由不同类的线程更新的值不会反射(reflect)在 java 中的另一个类中

转载 作者:行者123 更新时间:2023-12-01 16:34:08 24 4
gpt4 key购买 nike

我编辑了我的问题并使其变得非常简单。首先,同一个文件中有两个类HashMapClass:创建 ConccurentHashMap 的实例和NewThread:更新hashMap

public class HashMapClass {

public static volatile ConcurrentHashMap serverMap = new ConcurrentHashMap();

public static void main(String args[]) {

NewThread nt = new NewThread();
nt.start();
}
}

class NewThread extends Thread {

@Override
public void run() {
HashMapClass.serverMap.put("Ishan", new Integer(3));

System.out.println("Hash map is empty or not " + HashMapClass.serverMap.isEmpty());
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Logger.getLogger(NewThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

现在这里显示 hashMap 不为空,但如果我尝试从其他类 New Class 访问它,它显示为空

public class NewClass {

public static void main(String s[]) {
System.out.println("Hash map is empty or not " + HashMapClass.serverMap.isEmpty());
Set setMap = HashMapClass.serverMap.entrySet();
Iterator i = setMap.iterator();
for (int f = 0; i.hasNext(); ++f) {
Map.Entry me = (Map.Entry) i.next();
System.out.println("key is" + me.getKey() + "value is :" + me.getValue());
}
}

}

此类永远不会用数据更新。我希望现在很容易理解。

最佳答案

HashMap 不是线程安全的,因此如果需要跨线程共享 HashMap,则需要引入某种形式的同步。

或者,更简单的是,使用 ConcurrentHashMap which is thread safe .

另一方面,您可能会从阅读 Java tutorial on concurrency 中受益。 ,尤其是Memory consistency errors上的部分.

编辑

根据您的评论,请参阅一个简单的示例,我认为该示例可以隔离您的问题 - 我的机器上的输出是:

Hash map empty in main (1st attempt)? true
Hash map empty in run? false
Hash map empty in main (2nd attempt)? false

public class Test {

public static ConcurrentMap serverMap = new ConcurrentHashMap();

public static void main(String[] args) throws InterruptedException {
NewThread nt = new NewThread();
nt.start();
System.out.println("Hash map empty in main (1st attempt)? " + serverMap.isEmpty());
Thread.sleep(200);
System.out.println("Hash map empty in main (2nd attempt)? " + serverMap.isEmpty());
}

static class NewThread extends Thread {

@Override
public void run() {
serverMap.put("Ishan", new Integer(3));

System.out.println("Hash map empty in run? " + serverMap.isEmpty());
}
}
}

第二次编辑

您可以从另一个类应用相同的逻辑 - 只需确保调用 NewThread.start 并等待足够的时间(或者直接调用 NewThread.run,以便它在同一个线程中运行,您不必等待) :

public class NewClass {

public static void main(String s[]) throws InterruptedException {
new Test.NewThread().start();
System.out.println("Hash map is empty or not (1): " + Test.serverMap.isEmpty());
Thread.sleep(100);
System.out.println("Hash map is empty or not (2): " + Test.serverMap.isEmpty());
Set setMap = Test.serverMap.entrySet();
Iterator i = setMap.iterator();
for (int f = 0; i.hasNext(); ++f) {
Map.Entry me = (Map.Entry) i.next();
System.out.println("key is" + me.getKey() + "value is :" + me.getValue());
}
}
}

关于java - 由不同类的线程更新的值不会反射(reflect)在 java 中的另一个类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11230624/

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