Java中有一个HashMap,我们也都知道,改类型为线程不安全,会出现线程死循环问题,今天我们通过一个小代码来模拟一下HashMap产生死循环,同时描述一下现象:
代码:
java代码:
package com.wmmad.threadlocal.test;
import java.util.HashMap;
/**
* * @author madding.lip
*
* @date 2010.08.25
*
* <pre>
* 模拟HashMap循环现象
* </pre>
*/
public class HashMapInfiniteLoop {
private HashMap hash = new HashMap();
public HashMapInfiniteLoop() {
Thread t1 = new Thread() {
public void run() {
for (int i = 0; i < 50000; 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 < 50000; 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();
}
}