collection/src/test/java/androidx/collection/SimpleArrayMapTest.java
@Test
    public void testConcurrentModificationException() throws Exception {
        final int TEST_LEN_MS = 5000;
        System.out.println("Starting SimpleArrayMap concurrency test");
        mDone = false;
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (!mDone) {
                    try {
                        map.put(String.format(Locale.US, "key %d", i++), "B_DONT_DO_THAT");
                    } catch (ArrayIndexOutOfBoundsException e) {
                        // SimpleArrayMap is not thread safe, so lots of concurrent modifications
                        // can still cause data corruption
                        System.err.println("concurrent modification uncaught, causing indexing failure");
                        e.printStackTrace();
                    } catch (ClassCastException e) {
                        // cache corruption should not occur as it is hard to trace and one thread
                        // may corrupt the pool for all threads in the same process.
                        System.err.println("concurrent modification uncaught, causing cache corruption");
                        e.printStackTrace();
                        fail();
                    } catch (ConcurrentModificationException e) {
                    }
                }
            }
        }).start();
        for (int i = 0; i < (TEST_LEN_MS / 100); i++) {
            try {
                Thread.sleep(100);
                map.clear();
            } catch (InterruptedException e) {
            } catch (ArrayIndexOutOfBoundsException e) {
                System.err.println("concurrent modification uncaught, causing indexing failure");
            } catch (ClassCastException e) {
                System.err.println("concurrent modification uncaught, causing cache corruption");
                fail();
            } catch (ConcurrentModificationException e) {
            }
        }
        mDone = true;
    }
/**
     * Attempt to generate a ConcurrentModificationException in ArrayMap.
     */
Attempt to generate a ConcurrentModificationException in ArrayMap.
Attempt to generate a ConcurrentModificationException in ArrayMap .
@Test
    public void testConcurrentModificationException() throws Exception {
        final int TEST_LEN_MS = 5000;
        System.out.println("Starting SimpleArrayMap concurrency test");
        mDone = false;
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (!mDone) {
                    try {
                        map.put(String.format(Locale.US, "key %d", i++), "B_DONT_DO_THAT");
                    } catch (ArrayIndexOutOfBoundsException e) {
                        System.err.println("concurrent modification uncaught, causing indexing failure");
                        e.printStackTrace();
                    } catch (ClassCastException e) {
                        System.err.println("concurrent modification uncaught, causing cache corruption");
                        e.printStackTrace();
                        fail();
                    } catch (ConcurrentModificationException e) {
                    }
                }
            }
        }).start();
        for (int i = 0; i < (TEST_LEN_MS / 100); i++) {
            try {
                Thread.sleep(100);
                map.clear();
            } catch (InterruptedException e) {
            } catch (ArrayIndexOutOfBoundsException e) {
                System.err.println("concurrent modification uncaught, causing indexing failure");
            } catch (ClassCastException e) {
                System.err.println("concurrent modification uncaught, causing cache corruption");
                fail();
            } catch (ConcurrentModificationException e) {
            }
        }
        mDone = true;
    }
@ Test public void testConcurrentModificationException ( ) throws Exception { final int TEST_LEN_MS = 5000 ; System . out . println ( "Starting SimpleArrayMap concurrency test" ) ; mDone = false ; new Thread ( new Runnable ( ) { @ Override public void run ( ) { int i = 0 ; while ( ! mDone ) { try { map . put ( String . format ( Locale . US , "key %d" , i ++ ) , "B_DONT_DO_THAT" ) ; } catch ( ArrayIndexOutOfBoundsException e ) { System . err . println ( "concurrent modification uncaught, causing indexing failure" ) ; e . printStackTrace ( ) ; } catch ( ClassCastException e ) { System . err . println ( "concurrent modification uncaught, causing cache corruption" ) ; e . printStackTrace ( ) ; fail ( ) ; } catch ( ConcurrentModificationException e ) { } } } } ) . start ( ) ; for ( int i = 0 ; i < ( TEST_LEN_MS / 100 ) ; i ++ ) { try { Thread . sleep ( 100 ) ; map . clear ( ) ; } catch ( InterruptedException e ) { } catch ( ArrayIndexOutOfBoundsException e ) { System . err . println ( "concurrent modification uncaught, causing indexing failure" ) ; } catch ( ClassCastException e ) { System . err . println ( "concurrent modification uncaught, causing cache corruption" ) ; fail ( ) ; } catch ( ConcurrentModificationException e ) { } } mDone = true ; }
Attempt to generate a ConcurrentModificationException in ArrayMap.
Attempt to generate a ConcurrentModificationException in ArrayMap .
// SimpleArrayMap is not thread safe, so lots of concurrent modifications // can still cause data corruption // cache corruption should not occur as it is hard to trace and one thread // may corrupt the pool for all threads in the same process.