1.7 hashmap 多线程成环问题
总不是很明白。看了这个明白了一些,这里记录一下https://blog.csdn.net/maohoo/article/details/81531925
- 环境:
- jdk1.7.0_67
- 代码
public class Key {
private int value;
public Key(int value) {
this.value = value;
}
@Override
public int hashCode() {
return value%2;
}
}
private static HashMap<Key, Integer> map = new HashMap<Key, Integer>(4,0.75f);
public static void main(String[] args) {
map.put(new Key(9), 9);
map.put(new Key(17), 17);
map.put(new Key(1), 1);
new Thread("ThreadA") {
public void run() {
map.put(new Key(5), 33);
System.out.println(map+"ThreadA");
System.out.println(map.get(new Key(11)));
};
}.start();
new Thread("ThreadB") {
public void run() {
map.put(new Key(3), 33);
System.out.println(map+"ThreadB");
};
}.start();
}
- 设置断点(均在hashmap中)
- 两个断点设置条件均为:“ThreadA”.equals(Thread.currentThread().getName())||“ThreadB”.equals(Thread.currentThread().getName())
- 这里设置条件为"ThreadA".equals(Thread.currentThread().getName())
- 启动main方法
- 让A、B分别到达581行
- 放A到594行
- 放B到582行
- 放A到582
- 在A下的newTable值
- newTable[1]:1->17->1 出现循环
- 而且还丢值了
- 在这里还看到有关jps、jstack的命令:https://blog.csdn.net/majinggogogo/article/details/80036544?ops_request_misc=&request_id=&biz_id=102&utm_medium=distribute.pc_search_result.none-task-blog-2blogsobaiduweb~default-0