// tranfer()片段
// 这是在resize()中调用的方法,resize()就是HashMap扩容的方法
for (int j = 0; j < src.length; j++) {
Entry e = src[j];
if (e != null) {
src[j] = null;
do {
Entry next = e.next; //假设线程1停留在这里就挂起了,线程2登场
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
} while (e != null);
}
}
线程一:Thread1影响key1
线程二:Thread1影响key2
因为两条线程的影响,倒是出现循环的情况
出现问题的测试代码:
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
public class MyThread extends Thread {
/**
* 类的静态变量是各个实例共享的,因此并发的执行此线程一直在操作这两个变量
* 选择AtomicInteger避免可能的int++并发问题
*/
private static AtomicInteger ai = new AtomicInteger(0);
//初始化一个table长度为1的哈希表
private static HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(1);
//如果使用ConcurrentHashMap,不会出现类似的问题
// private static ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<Integer, Integer>(1);
public void run() {
while (ai.get() < 100000) { //不断自增
map.put(ai.get(), ai.get());
ai.incrementAndGet();
}
System.out.println(Thread.currentThread().getName() + "线程即将结束");
}
public static void main(String[] args) {
MyThread t0 = new MyThread();
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
MyThread t4 = new MyThread();
MyThread t5 = new MyThread();
MyThread t6 = new MyThread();
MyThread t7 = new MyThread();
MyThread t8 = new MyThread();
MyThread t9 = new MyThread();
t0.start();
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
t7.start();
t8.start();
t9.start();
}
}
void addEntry(int hash, K key, V value, int bucketIndex) {
//多个线程操作数组的同一个位置
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}