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?
ArrayListas a whole is not thread safe, so no, that method is probably not thread safe either.