HashMap陷入死循环的例子

时间:2023-03-09 01:44:10
HashMap陷入死循环的例子
//使用这个例子可以模拟HashMap陷入死循环的效果,可能需要执行多次才会出现。
1 package com.hanzi; import java.util.HashMap; public class HashMapInfiniteLoop {
private HashMap hash = new HashMap(500); public HashMapInfiniteLoop() {
Thread t1 = new Thread() {
public void run() {
for (int i = 0; i < 500000; i++) {
hash.put(new Integer(i), Integer.valueOf(i));
}
System.out.println("t1 over");
}
};
Thread t2 = new Thread() {
public void run() {
for (int i = 0; i < 500000; i++) {
hash.put(new Integer(i),Integer.valueOf(i));
}
System.out.println("t2 over");
}
};
t1.start();
t2.start();
} public static void main(String[] args) {
new HashMapInfiniteLoop();
}
}