Below code throw outofBound Exception on arraylist if executed continuously(immediately)2-3 times.
I am aware that this exception can be avoided by synchronizing alterlist() methods or synchronized bocks using different objectslock1 and lock2 I have created. I want to understand why this exception is thrown. Arraylist auto increase its size when needed.
Can anyone explain what is happening behind the scene
Exception:
Starting...
Exception in
thread "Thread-1" java.lang.ArrayIndexOutOfBoundsException: 25
at java.util.ArrayList.add(ArrayList.java:352)
at com.suri.threads.MultipleLocks.alterList2(MultipleLocks.java:75)
at com.suri.threads.MultipleLocks.process(MultipleLocks.java:56)
at com.suri.threads.MultipleLocks$2.run(MultipleLocks.java:36)
at java.lang.Thread.run(Thread.java:662)
Time taken : 2067
List 1 size : 1013 List 2 Size : 1013