473,382 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

ArrayList, null and Threads

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?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
3 1836
ArrayList isn't threadsafe, therefore you should use a threadsafe wrapper
which is returned by calling:

ArrayList.Synchronized(ArrayList list);

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"vooose" <no****@microsoft.com> schrieb im Newsbeitrag
news:OY**************@TK2MSFTNGP09.phx.gbl...
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?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
cody, Thanks for your reply.

Suppose you want a thread-safe ArrayList, as a class variable, you would
then use

private ArrayList myArr = ArrayList.Synchronized(new ArrayList());

?
Not sure why it just isnt synchronized to begin with.

I still can't figure out, even with all the threads in the world
accessing a non-synchronized arraylist, how it could set a member of the
arraylist to null using only Add( ) and Remove(this). Maybe just not
worth wondering about.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
> I still can't figure out, even with all the threads in the world
accessing a non-synchronized arraylist, how it could set a member of the
arraylist to null using only Add( ) and Remove(this). Maybe just not
worth wondering about.

Multiple threads writing in the same object can have unexpected and
undefined results so it maybe really isn't worth to think about which kind
of undefined results there might be.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: roy | last post by:
The following is a simple example I wrote. I want function makenull() to set the arraylist null. However, in this example, after coming back from makenull() function, s is still not null. I got...
9
by: Lloyd Dupont | last post by:
Does any of you have the slightest ideas of why you can't modify an array list while in a foreach of its element. I wrote my own collection and I'm trying to add the same behavior but I don't...
10
by: Eric | last post by:
I'm looking at this page in the MSDN right here: ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist classsynchronizedtopic2.htm (or online here:...
14
by: Mike | last post by:
I had a question about threading and access to private class variables. I am developing a windows service which has a class inside of it which will receive various asynchronous calls to it via...
5
by: vooose | last post by:
Consider an ArrayList that you add an object to: ArrayList list = new ArrayList(); if(myObj != null) list.Add(myObj); else throw new Exception("obj cannot be null"); Some time down the...
0
by: Jeremy Chapman | last post by:
I have included below virtually all the code to a control I'm trying to build. My issue is that an array list property in my control does not get persisted properly to the aspx page code in design...
1
by: KK | last post by:
Dear All I have a class whose methods are getting called from multiple threads in my application. For example class DataDistribution { private ArrayList datset; public DataDistribution() {...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.