collection/collection/src/test/java/androidx/collection/ArrayMapTest.java
/**
     * Attempt to generate a ConcurrentModificationException in ArrayMap.
     * <p>
     * ArrayMap is explicitly documented to be non-thread-safe, yet it's easy to accidentally screw
     * this up; ArrayMap should (in the spirit of the core Java collection types) make an effort to
     * catch this and throw ConcurrentModificationException instead of crashing somewhere in its
     * internals.
     */
Attempt to generate a ConcurrentModificationException in ArrayMap.

ArrayMap is explicitly documented to be non-thread-safe, yet it's easy to accidentally screw
this up; ArrayMap should (in the spirit of the core Java collection types) make an effort to
catch this and throw ConcurrentModificationException instead of crashing somewhere in its
internals.
Attempt to generate a ConcurrentModificationException in ArrayMap . ArrayMap is explicitly documented to be non - thread - safe yet it ' s easy to accidentally screw this up ; ArrayMap should ( in the spirit of the core Java collection types ) make an effort to catch this and throw ConcurrentModificationException instead of crashing somewhere in its internals .
@Test
    public void testConcurrentModificationException() throws Exception {
        final int testLenMs = 5000;
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (mMap != null) {
                    try {
                        mMap.put(String.format("key %d", i++), "B_DONT_DO_THAT");
                    } catch (ArrayIndexOutOfBoundsException e) {
                        fail(e.getMessage());
                    } catch (ClassCastException e) {
                        fail(e.getMessage());
                    } catch (ConcurrentModificationException e) {
                        System.out.println("[successfully caught CME at put #" + i
                                + " size=" + (mMap == null ? "??" : String.valueOf(mMap.size()))
                                + "]");
                    }
                }
            }
        }).start();
        for (int i = 0; i < (testLenMs / 100); i++) {
            try {
                Thread.sleep(100);
                mMap.clear();
            } catch (InterruptedException e) {
            } catch (ArrayIndexOutOfBoundsException e) {
                fail(e.getMessage());
            } catch (ClassCastException e) {
                fail(e.getMessage());
            } catch (ConcurrentModificationException e) {
                System.out.println(
                        "[successfully caught CME at clear #"
                                + i + " size=" + mMap.size() + "]");
            }
        }
        mMap = null; 
        System.out.println();
    }
@ Test public void testConcurrentModificationException ( ) throws Exception { final int testLenMs = 5000 ; new Thread ( new Runnable ( ) { @ Override public void run ( ) { int i = 0 ; while ( mMap != null ) { try { mMap . put ( String . format ( "key %d" , i ++ ) , "B_DONT_DO_THAT" ) ; } catch ( ArrayIndexOutOfBoundsException e ) { fail ( e . getMessage ( ) ) ; } catch ( ClassCastException e ) { fail ( e . getMessage ( ) ) ; } catch ( ConcurrentModificationException e ) { System . out . println ( "[successfully caught CME at put #" + i + " size=" + ( mMap == null ? "??" : String . valueOf ( mMap . size ( ) ) ) + "]" ) ; } } } } ) . start ( ) ; for ( int i = 0 ; i < ( testLenMs / 100 ) ; i ++ ) { try { Thread . sleep ( 100 ) ; mMap . clear ( ) ; } catch ( InterruptedException e ) { } catch ( ArrayIndexOutOfBoundsException e ) { fail ( e . getMessage ( ) ) ; } catch ( ClassCastException e ) { fail ( e . getMessage ( ) ) ; } catch ( ConcurrentModificationException e ) { System . out . println ( "[successfully caught CME at clear #" + i + " size=" + mMap . size ( ) + "]" ) ; } } mMap = null ; System . out . println ( ) ; }
Attempt to generate a ConcurrentModificationException in ArrayMap.
Attempt to generate a ConcurrentModificationException in ArrayMap .
// will stop other thread