473,326 Members | 2,148 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,326 software developers and data experts.

Is a readonly collection thread safe

Hi guys

I have in my application many collections (HashTable and
ArrayList)loaded with configuration data, that means they are loaded
at startup and then they are accessed only for read-only (items are
only retrieved, never added, removed or replaced).
To be thread-safe after startup do I need to made them Synchronized
(and so slower), or are they thread safe in such case ?

Thanks in advance
David
Jul 21 '05 #1
2 5158
Hi David,

I'd still recommend sticking to the safe way. As an alternative, introduce a
simple data holder class like this:

public class AppSettings
{
private int _setting1;
private string _setting2;

public AppSettings()
{
// Reads settings from some permanent storage and populates _setting1
and _setting2
}

public int Setting1
{
get
{
return _setting1;
}
}

public string Setting2
{
get
{
return _setting2;
}
}
}

and instantiate it on start-up and then safely access this data holder class
without any synchronization. You will most likely wish to turn this class
into a singleton - and that would be right decision - I have omitted any
singleton support for the sake of brevity.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"David" <pi***@hotmail.com> wrote in message
news:9c**************************@posting.google.c om...
Hi guys

I have in my application many collections (HashTable and
ArrayList)loaded with configuration data, that means they are loaded
at startup and then they are accessed only for read-only (items are
only retrieved, never added, removed or replaced).
To be thread-safe after startup do I need to made them Synchronized
(and so slower), or are they thread safe in such case ?

Thanks in advance
David


Jul 21 '05 #2
MSDN:
"By default, Collections classes are generally not thread safe. Multiple
readers can safely read the collection; however, any modification to the
collection produces undefined results for all threads that access the
collection, including the reader threads."

So, as long as you only read the collections, it's ok. Generally the "thread
safety" is defined by the synchronization of threads when accessing a common
resource, producing/consuming in the same time, but as long as no one
modifies it it's ok.

Anyhow Hashtable is thread safe.
--
Horatiu Ripa

"David" <pi***@hotmail.com> wrote in message
news:9c**************************@posting.google.c om...
Hi guys

I have in my application many collections (HashTable and
ArrayList)loaded with configuration data, that means they are loaded
at startup and then they are accessed only for read-only (items are
only retrieved, never added, removed or replaced).
To be thread-safe after startup do I need to made them Synchronized
(and so slower), or are they thread safe in such case ?

Thanks in advance
David

Jul 21 '05 #3

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

Similar topics

3
by: Grandma Wilkerson | last post by:
Hi, The documentation states that enumeration through a collection is inherently NOT thread-safe, since a thread which added/removed an item from said collection could screw up the thread that...
3
by: BoloBaby | last post by:
All, I believe I am having a threading problem. Class "BELights" is part of a larger DLL that is used by my main application. A user control (of type BESeat) within the main application raises...
0
by: fred | last post by:
I need some help in trying to understand how to make myCollection (inherited from CollectionBase) multithread safe. Taking my implementation of the Add Sub and a readonly property Item. Public...
11
by: Eric | last post by:
I have a VB.net dll project with a class that is a singleton. I've been using this in winform apps without any problems. I would like to use this same dll in a web form project but my singleton...
2
by: David | last post by:
Hi guys I have in my application many collections (HashTable and ArrayList)loaded with configuration data, that means they are loaded at startup and then they are accessed only for read-only...
7
by: Alexander Walker | last post by:
Hello I want to get the value of a property of a control from a thread other than the thread the control was created on, as far as I can see this is not the same as invoking an operation on a...
8
by: Senna | last post by:
Hi I have some questions about this code, http://code.doria.se/?c=18. 1. Will the Adapter class be thread-safe? 2. In the #region Way 1 I use lock(_lockobj) every time I go about and do...
14
by: Jeff S. | last post by:
In a Windows Forms application I plan to have a collection of structs - each of which contains a bunch of properties describing a person (e.g., LastName, FirstName, EmployeeID, HomeAddress,...
12
by: David | last post by:
Below are three classes for a console application. If put into three separate files, the sub main() will launch multiple threads adding and removing the same value. At the end we expect the value...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.