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

标签 java multithreading

我编辑了我的问题并使其变得非常简单。 首先,同一个文件中有两个类 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/

相关文章:

java - Android 应用程序在使用 JNI 后随机崩溃

java - 使用 Java 查找天气

java - 在 onPostExecute 中的 notify() 之前没有被线程锁定的对象

python - 如何线程化一个 wxpython 进度条 GUI?

java - 使用有状态 session Bean (EJB)

java - 如何从 jar 文件 @autowire 实现类?

c# - 如何知道哪个方法启动了工作线程

Java 线程组织指南

c# - 程序打不开,aforge camera一直在运行,线程问题

java - 在android上实现自动颜色(photoshop工具)