1
如果两个线程尝试将(键,值)放入同一个映射中传递给线程的构造函数。我可能遇到什么样的线程问题?由两个线程并发修改HashMap
public class App {
public static void main(String[] args) throws JMSException {
Map<String, String> map = new HashMap<String, String>();
map.put("5", "fnc");
Thread t1 = new App().new T(map);
Thread t2 = new App().new T(map);
t1.start();
t2.start();
}
class T extends Thread {
private Map<String, String> map;
public T(Map<String, String> map) {
this.map = map;
}
public void run() {
// put 100s of keys in map here
map.put("1", "abc");
// put other keys
}
}
}
对Oracle HashMap实现的并发读取和写入操作可能会导致挂起的线程(由于底层实现,很容易使其陷入无限循环)。请参阅http://stackoverflow.com/a/1068213/83695 – andersoj