【发布时间】:2012-07-23 05:00:14
【问题描述】:
我有课
class A {
private static HashMap hash;
public static void setHash(HashMap hash)
{
this.hash=hash;
}
public static HashMap getHash()
{
return hash;
}
}
我有两个线程线程 A 和线程 B
public class ThreadA implements Runnable
{
private HashMap hash;
public ThreadA(HashMap hash)
{
this.hash = hash
}
}
同样,对于 ThreadB
在主课中,我执行以下操作
main()
{
// inserted some values in hashmap
Thread t1 = new Thread(new ThreadA(hash));
Thread t2 = new Thread(new ThreadB(hash));
}
另外一个线程 C 使用 hash 的 setter 方法来更改 hashmap。新值没有反映在线程 A 和 B 中。
可能是什么问题呢?
谢谢!
【问题讨论】:
-
我在课堂上只看到一个二传手
A;您究竟是如何更改ThreadA和ThreadB中的 HashMap 实例的? -
查看 ConcurrentHashMaps 的线程安全性。 java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/…
标签: java multithreading thread-safety