My Previous question brought me to this question.
Is add function of ArrayList thread safe?
I made a sample application with following classes
import java.util.ArrayList;
import java.util.List;
public class ThreadTest
{
public static List<DummyObject> list = null;
public static boolean isLoaded = false;
public static void main(String [] args)
{
MyThread t1 = new MyThread(1);
MyThread t2 = new MyThread(2);
t1.start();
t2.start();
}
public static void loadObject(){
if(isLoaded){
return;
}
isLoaded = false;
try{
list = new ArrayList<DummyObject>();
for(int i=0;i<10;i++){
list.add(i,new DummyObject());
}}
catch(Exception e){
e.printStackTrace();
}
isLoaded = true;
}
}
These are my threads
public class MyThread extends Thread
{
int threadNumber ;
public MyThread(int threadNumber)
{
this.threadNumber = threadNumber;
}
@Override
public void run()
{
try {
sleep(10-threadNumber);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Running Thread: " + threadNumber);
ThreadTest.loadObject();
if(ThreadTest.isLoaded){
System.out.println(ThreadTest.list);
for(int i=0;i<ThreadTest.list.size();i++){
if(ThreadTest.list.get(i)==null){
throw new NullPointerException();
}
}
}else {
try {
sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
This is my dummy class
public class DummyObject {
}
Even though I wasn't able to replicate the Null Pointer Exception
that I got on my previous question , I sometimes get this error
Exception in thread "Thread-1" java.lang.IndexOutOfBoundsException: Index: 1, Size: 10
at java.util.ArrayList.add(ArrayList.java:367)
at ThreadTest.loadObject(ThreadTest.java:25)
at MyThread.run(MyThread.java:20)
Form ArrayList Code this is the line thats throwing an error:
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
But as we can see from Exception index is 1 and size is 10, so there is no way that if condition is satisfied. So is my assumption correct that add function of arrayList is thread unsafe or is something else going on here?
From the documentation:
(This class is roughly equivalent to Vector, except that it is unsynchronized.)
You need to either implement the synchronization yourself, or better yet, use a synchronized container like Vector.
In the case of your code, you have 2 threads running the same piece of code (loadObject
) in which several static values are accessed/modified. You need to make sure that each access is done in a synchronized manner.
You have 2 threads, thus you allocate twice the ThreadTest.list
field, so one of the allocation is useless, but more importantly, there might be some values inserted in that list before it is lost, so these values become lost as well.
You should make sure that the list is not allocated before allocating it.
You could also have problem with the isLoaded field, leading to more than 10 elements in your list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With