-3

I am getting ArrayIndexOutOfBoundException while checking thread safety of ArrayList. We are getting exception.We are getting exception after that we are getting the output but while using vector we are getting proper size. While I am using ArrayList I am getting the below exception:

Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: 1851 at java.util.ArrayList.add(ArrayList.java:465) at VectorTut.lambda$0(VectorTut.java:27) at java.lang.Thread.run(Thread.java:750) Size of the list: 1919 

After that I get the size of the list.

But when I am providing Vector instead of ArrayList I am getting the proper output. So, my question is how to resolve the exception? Is there anyway to avoid the exception or this is its natural way. Kindly please reply.

import java.util.ArrayList; import java.util.LinkedList; import java.util.Vector; public class VectorTut { public static void main(String[] args) { // Vector<Integer> vector = new Vector<>(5,4); // vector.add(1); // vector.add(2); // vector.add(3); // vector.add(4); // vector.add(5); // System.out.println(vector.capacity()); // vector.add(6); // System.out.println(vector.capacity()); // LinkedList<Integer> linkedList = new LinkedList<>(); // linkedList.add(1); // linkedList.add(2); // linkedList.add(3); // Vector<Integer> vector2 = new Vector<>(linkedList); // System.out.println(vector2); ArrayList<Integer> arrayList = new ArrayList<>(); Thread t1 = new Thread(()->{ for(int i=0;i<1000;i++){ arrayList.add(i); } }); Thread t2 = new Thread(()->{ for(int i=0;i<1000;i++){ arrayList.add(i); } }); t1.start(); t2.start(); try { t1.join(); t2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Size of the list: "+arrayList.size()); } }```` 
New contributor
MANAS HALDER is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
10
  • 7
    javadoc of ArrayList: "Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally." (original emphasis) -- and javadoc of Vector: "Unlike the new collection implementations, Vector is synchronized."
    – user85421
    Commented22 hours ago
  • ( you can try using Collections.synchronizedList as suggested by above mentioned javadoc, at least to confirm if synchronization is the problem )
    – user85421
    Commented22 hours ago
  • 5
    @MANASHALDER You have correctly done the job and I'm confused as to what your question is about. As you said, you were checking the thread safety of an ArrayList and you have determined that it is not in any way safe to modify an ArrayList from more than one thread. That is indeed the correct conclusion.Commented21 hours ago
  • 3
    Yea. If you use a non-thread-safe class in from multiple threads without any external synchronization, weird things can happen. In this case, it was an unexpected exception. In other cases, it can be worse.
    – Stephen C
    Commented19 hours ago
  • 3
    You resolve this by not modifying the ArrayList from different threads. There is no way to suggest an alternative for a piece of code whose only purpose is to do the wrong thing. If you had an actual goal, we could suggest ways to achieve the goal.
    – Holger
    Commented5 hours ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.