Java并发编程

Java并发编程 - jdk1.7下HashMap死循环问题

2019-03-06  本文已影响38人  HRocky

这篇文章以一个示例出发研究多线程环境下infinite loop问题。

代码示例

为了调试方便(方便查看局部变量的调试值以及在研究的过程中打印日志之类的需求),这里我写了一个SimpleHashMap类,此类为JDK1.7中HashMap源代码的拷贝,只提供了put方法,代码示例如下:

百分之百的源代码拷贝,不用担心与HashMap的put方法功能有差异。

之所以要这样做是因为我想写个测试代码,通过只跑测试代码就能得到程序异常的效果,研究期间通过在这个SimpleHashMap类上加入休眠等很多乱七八糟的代码,最后未果, 这个类的put方法与HashMap中的无异,索性就保留了,基于此代码测试。

SimpleMap.java

public interface SimpleMap<K, V> {

    interface Entry<K,V> {

        K getKey();

        V getValue();

        V setValue(V value);

        boolean equals(Object o);

        int hashCode();
    }

}

SimpleHashMap.java

import java.io.Serializable;
import java.util.Objects;

public class SimpleHashMap<K,V> implements Cloneable, Serializable {

    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

    static final float DEFAULT_LOAD_FACTOR = 0.75f;

    static final int MAXIMUM_CAPACITY = 1 << 30;

    final float loadFactor;

    int threshold;

    transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;

    static final Entry<?,?>[] EMPTY_TABLE = {};

    static final int ALTERNATIVE_HASHING_THRESHOLD_DEFAULT = Integer.MAX_VALUE;

    transient int hashSeed = 0;

    transient int modCount;

    transient int size;

    public SimpleHashMap() {
        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
    }

    public SimpleHashMap(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                    initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                    loadFactor);

        this.loadFactor = loadFactor;
        threshold = initialCapacity;
        init();
    }

    void init() {
    }

    public V put(K key, V value) {
        if (table == EMPTY_TABLE) {
            inflateTable(threshold);
        }
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key);
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }

    public V get(Object key) {
        if (key == null)
            return getForNullKey();
        Entry<K,V> entry = getEntry(key);

        return null == entry ? null : entry.getValue();
    }

    final Entry<K,V> getEntry(Object key) {
        if (size == 0) {
            return null;
        }

        int hash = (key == null) ? 0 : hash(key);
        for (Entry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                return e;
        }
        return null;
    }

    private V getForNullKey() {
        if (size == 0) {
            return null;
        }
        for (Entry<K,V> e = table[0]; e != null; e = e.next) {
            if (e.key == null)
                return e.value;
        }
        return null;
    }

    private V putForNullKey(V value) {
        for (Entry<K,V> e = table[0]; e != null; e = e.next) {
            if (e.key == null) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }
        modCount++;
        addEntry(0, null, value, 0);
        return null;
    }

    void addEntry(int hash, K key, V value, int bucketIndex) {
        if ((size >= threshold) && (null != table[bucketIndex])) {
            resize(2 * table.length);
            hash = (null != key) ? hash(key) : 0;
            bucketIndex = indexFor(hash, table.length);
        }

        createEntry(hash, key, value, bucketIndex);
    }

    void createEntry(int hash, K key, V value, int bucketIndex) {
        Entry<K,V> e = table[bucketIndex];
        table[bucketIndex] = new Entry<>(hash, key, value, e);
        size++;
    }

    void resize(int newCapacity) {
        Entry[] oldTable = table;
        int oldCapacity = oldTable.length;
        if (oldCapacity == MAXIMUM_CAPACITY) {
            threshold = Integer.MAX_VALUE;
            return;
        }

        Entry[] newTable = new Entry[newCapacity];
        transfer(newTable, initHashSeedAsNeeded(newCapacity));
        table = newTable;
        threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
    }

    void transfer(Entry[] newTable, boolean rehash) {
        int newCapacity = newTable.length;
        for (Entry<K,V> e : table) {
            while(null != e) {
                Entry<K,V> next = e.next;
                if (rehash) {
                    e.hash = null == e.key ? 0 : hash(e.key);
                }
                int i = indexFor(e.hash, newCapacity);
                e.next = newTable[i];
                newTable[i] = e;
                e = next;
            }
        }
    }

    static int indexFor(int h, int length) {
        // assert Integer.bitCount(length) == 1 : "length must be a non-zero power of 2";
        return h & (length-1);
    }

    final int hash(Object k) {
        int h = hashSeed;
        if (0 != h && k instanceof String) {
            return sun.misc.Hashing.stringHash32((String) k);
        }

        h ^= k.hashCode();

        // This function ensures that hashCodes that differ only by
        // constant multiples at each bit position have a bounded
        // number of collisions (approximately 8 at default load factor).
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }

    private void inflateTable(int toSize) {
        // Find a power of 2 >= toSize
        int capacity = roundUpToPowerOf2(toSize);

        threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
        table = new Entry[capacity];
        initHashSeedAsNeeded(capacity);
    }

    private static int roundUpToPowerOf2(int number) {
        // assert number >= 0 : "number must be non-negative";
        return number >= MAXIMUM_CAPACITY
                ? MAXIMUM_CAPACITY
                : (number > 1) ? Integer.highestOneBit((number - 1) << 1) : 1;
    }

    final boolean initHashSeedAsNeeded(int capacity) {
        boolean currentAltHashing = hashSeed != 0;
        boolean useAltHashing = sun.misc.VM.isBooted() &&
                (capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
        boolean switching = currentAltHashing ^ useAltHashing;
        if (switching) {
            hashSeed = useAltHashing
                    ? sun.misc.Hashing.randomHashSeed(this)
                    : 0;
        }
        return switching;
    }

    private static class Holder {

        /**
         * Table capacity above which to switch to use alternative hashing.
         */
        static final int ALTERNATIVE_HASHING_THRESHOLD;

        static {
            String altThreshold = java.security.AccessController.doPrivileged(
                    new sun.security.action.GetPropertyAction(
                            "jdk.map.althashing.threshold"));

            int threshold;
            try {
                threshold = (null != altThreshold)
                        ? Integer.parseInt(altThreshold)
                        : ALTERNATIVE_HASHING_THRESHOLD_DEFAULT;

                // disable alternative hashing if -1
                if (threshold == -1) {
                    threshold = Integer.MAX_VALUE;
                }

                if (threshold < 0) {
                    throw new IllegalArgumentException("value must be positive integer.");
                }
            } catch(IllegalArgumentException failed) {
                throw new Error("Illegal value for 'jdk.map.althashing.threshold'", failed);
            }

            ALTERNATIVE_HASHING_THRESHOLD = threshold;
        }
    }

    static class Entry<K,V> implements SimpleMap.Entry<K,V> {
        final K key;
        V value;
        Entry<K,V> next;
        int hash;

        /**
         * Creates new entry.
         */
        Entry(int h, K k, V v, Entry<K,V> n) {
            value = v;
            next = n;
            key = k;
            hash = h;
        }

        public final K getKey() {
            return key;
        }

        public final V getValue() {
            return value;
        }

        public final V setValue(V newValue) {
            V oldValue = value;
            value = newValue;
            return oldValue;
        }

        public final boolean equals(Object o) {
            if (!(o instanceof SimpleMap.Entry))
                return false;
            SimpleMap.Entry e = (SimpleMap.Entry)o;
            Object k1 = getKey();
            Object k2 = e.getKey();
            if (k1 == k2 || (k1 != null && k1.equals(k2))) {
                Object v1 = getValue();
                Object v2 = e.getValue();
                if (v1 == v2 || (v1 != null && v1.equals(v2)))
                    return true;
            }
            return false;
        }

        public final int hashCode() {
            return Objects.hashCode(getKey()) ^ Objects.hashCode(getValue());
        }

        public final String toString() {
            return getKey() + "=" + getValue();
        }

        /**
         * This method is invoked whenever the value in an entry is
         * overwritten by an invocation of put(k,v) for a key k that's already
         * in the HashMap.
         */
        void recordAccess(SimpleHashMap<K,V> m) {
        }

        /**
         * This method is invoked whenever the entry is
         * removed from the table.
         */
        void recordRemoval(SimpleHashMap<K,V> m) {
        }
    }

}

下面是测试代码类:

HashMapInfiniteLoop.java


public class HashMapInfiniteLoop {

    private static SimpleHashMap<Integer,String> hashMap = new SimpleHashMap<Integer,String>(2,0.75f);

    public static void main(String[] args) throws InterruptedException {

        hashMap.put(4,"C");

        Thread thread1 = new Thread("Thread1") {
            public void run() {
                hashMap.put(-2145873729, "B");
                hashMap.put(-432181405, "A");
                hashMap.put(8, "D");
            };
        };

        Thread thread2 = new Thread("Thread2") {
            public void run() {
                hashMap.put(8, "E");
            };
        };

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();
    }
}

你可能会发现上面代码中有一些奇怪的key值,这些key值很关键,稍后会做说明。

调试准备

我是基于IDEA进行开发的,这里是测试多线程环境,所以读者要对IDEA多线程调试方式熟悉,不熟悉请查阅相关资料。

打断点

代码中需要打断点的地方,如下列图示。

HashMapInfiniteLoop类断点

HashMapInfiniteLoop断点.png

SimpleHashMap断点

SimpleHashMap断点.png

调试

第一步:运行HashMapInfiniteLoop插入C、B、A

以Debug的方式运行HashMapInfiniteLoop,确保下列三行代码按顺序执行完成,然后断点停留在HashMapInfiniteLoop的第一个断点上:

hashMap.put(4,"C");
hashMap.put(-2145873729, "B");
hashMap.put(-432181405, "A");

线程调度的不确定性不能百分百确定Thread-1线程先与Thread-2执行,但是这里我们的测试要做这个保证,可喜的是我运行代码总是这样执行的。若你调试的时候不是如此,可以在HashMapInfiniteLoop图示的第一个代码之上打上多个断点,然后通过多线程调试方式确保上面的执行顺序。

下面以图示的方式来说明三步代码后Map内存内容。


C-B-A.png

第二步:调试执行Thread1线程

A. 第一处停歇

1-1.png

B. 第二处停歇

1-2.png

单步执行,直到next的值为B后停止调试Thread1线程。

1-3.png

第三步:调试执行Thread2线程

A. 第一步停歇

2-1.png

B. 第二步停歇

2-2.png

单步执行,直到for循环完成,有如图示结果:

2-3.png

通过下面的图说明,Thread2线程执行后Map内存结果。

Thread2插入D.png

从上图可以看到Thread2执行之后,A和B存在同一个index的bucket中,关系然后是链表关系。

这是我们模拟中非常关键的一个保证

我们知道HashMap扩容计算index,跟数组的长度是有关联的:

int hash = hash(key);
int i = indexFor(hash, table.length);

扩容之后还有有相同的index,那么就必须保证存入Map中的两个对象的key值,在数组长度扩大之后还能通过hash计算得到相同的值。

我们的测试代码是这样保证的,这里就回答了上面说的为什么测试的代码中的key看起来很奇怪的问题了。

下面是获取满足这个条件的key值的类。

KeyTool.java

import java.util.Random;

public class KeyTool {

    public static void main(String[] args) {
        test();
    }

    static void test() {
        int a = 0;
        int b = 0;
        Random random = new Random();
        do {
            a = random.nextInt();
            b = random.nextInt();
        } while (!((indexFor(hash(a), 4) == indexFor(hash(b), 4)) && (indexFor(hash(a), 8) == indexFor(hash(b), 8))));

        System.out.println(a);
        System.out.println(b);
    }

    static int indexFor(int h, int length) {
        // assert Integer.bitCount(length) == 1 : "length must be a non-zero power of 2";
        return h & (length-1);
    }

    final static int hash(Object k) {
        int h = 0;
        if (0 != h && k instanceof String) {
            return sun.misc.Hashing.stringHash32((String) k);
        }

        h ^= k.hashCode();

        // This function ensures that hashCodes that differ only by
        // constant multiples at each bit position have a bounded
        // number of collisions (approximately 8 at default load factor).
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }

}

indexFor和hash方法都是从HashMap源码中拷贝过来的。

第四步:返回Thread1暂停出重新调试

回到Thread1暂停出,我们会发现A和B对象,数据有所不同了,如图所示:

Thread2执行后A和B的变化.png

这个变化跟我们上面Thread2执行完后,内存结果的变化一致。A挂在了B下面。

在我们继续单步执行之前,我们要对一点很清楚,那就是Thread2完成的操作,Thread1是完全不知晓的,Thread1继续做自己的扩容操作,它要做的还是把数据转移到一个长度为8的空表中。

A. 第一次单步执行

3-1.png

可以看到新表的index=6的位置存放了A对象,变量e的值变成了B对象。

内存图示如下:

3-1内存图示.png

B. 第二次单步执行

3-2.png

可以看到新表的index=6的位置又存放为B对象了,变量e变成了A对象。

内存图示如下:

3-2内存图示.png

C. 第三步单步执行

3-3.png

可以看到可以看到新表的index=6的位置又存放为A对象了,变量e变成了null,循环结束。

现在就可以看到了A.next = B,并且B.next = A, 形成了一个闭环。

Thread1线程执行结束,将新表赋值给Map的table属性

table = newTable;

那么通过我们这样的多线程调试,最终Map的内存图示就是这样:

3-3内存图示.png

这样的结构,当迭代获取Map数据的时候就会出现是循环,也就是出现infinite loop问题!!!!

总结

HashMap中,出现bucket中链表紧连的某些对象的key在扩容之后还是计算出相同的index,会被放入到新表中的同一个bucket,这样在多线程环境下,某一线程刚好进行链表数据移动操作,此时另一个线程被调度执行并完成了链表的数据移动操作,就会在新表中形成一个环形链,从而导致获取数据的时候出现死循环问题。

上一篇 下一篇

猜你喜欢

热点阅读