ArrayMap java.lang.ArrayIndexOutOfBoundsException

错误堆栈:

java.lang.ArrayIndexOutOfBoundsException: length=0; index=1
        at android.support.v4.util.SimpleArrayMap.indexOf(SimpleArrayMap.java:83)
        at android.support.v4.util.SimpleArrayMap.put(SimpleArrayMap.java:381)

示例错误代码:

final ArrayMap testArrayMap = new ArrayMap();

        new  Thread("线程1"){
            @Override
            public void run() {
                super.run();
                while (true){
                    testArrayMap.put("aaa","fffff");
                }
            }

        }.start();

        new  Thread("线程2"){
            @Override
            public void run() {
                super.run();
                while (true){
                    testArrayMap.clear();
                }
            }

        }.start();

错误原因:
ArrayMap 不支持多行程,请在一个线程里面使用。不然会导致数组越界。如果想在多线程里面使用,请使用HasTable。

google 的 ArrayMap 本身就没有支持多线程,在最新的support 包里面,如果你还多线程使用ArrayMap,就会throw CME (ConcurrentModificationException)

posted @ 2018-11-13 13:54  有点理想的码农  阅读(779)  评论(0编辑  收藏  举报