V
vooose
Consider a class, ClassA that is used to perform a task using multiple
threads...ie
ClassA aObj = new ClassA(param1);
Thread t = new Thread(new ThreadStart(aObj.DoStuff));
anArrayList.Add(aObj);
The above may be called in many places, so the size of anArrayList
grows.
Now suppose the last two lines of DoStuff are:
anArrayList[0].toString();
anArrayList.Remove(this);
The Error? Sometimes I get ArrayIndexOutOfBounds on anArrayList[0] which
I would have thought was impossible given that an object is only ever
removed from ArrayList BEFORE it is null. Also as I am using
Remove(this) I would have thought that Remove( ) can't get confused
about removing the wrong index.
What I had to do to seemingly fix this was lock access to the arraylist
when calling Add( ) or Remove( ), just a step I didnt understand. Does
anyone know why?
threads...ie
ClassA aObj = new ClassA(param1);
Thread t = new Thread(new ThreadStart(aObj.DoStuff));
anArrayList.Add(aObj);
The above may be called in many places, so the size of anArrayList
grows.
Now suppose the last two lines of DoStuff are:
anArrayList[0].toString();
anArrayList.Remove(this);
The Error? Sometimes I get ArrayIndexOutOfBounds on anArrayList[0] which
I would have thought was impossible given that an object is only ever
removed from ArrayList BEFORE it is null. Also as I am using
Remove(this) I would have thought that Remove( ) can't get confused
about removing the wrong index.
What I had to do to seemingly fix this was lock access to the arraylist
when calling Add( ) or Remove( ), just a step I didnt understand. Does
anyone know why?